SPB Git

spb/forge Public MIT

Forge — LLM training from scratch in pure C++20 + Metal on Apple Silicon.

C++ 61.2% C 23% Python 7.6% TeX 7.2% CMake 1.1%
18.0 KB · 387 lines cpp
Raw Blame History
1//-------------------------------------------------------------------------------------------------------------------------------------------------------------2//3// Foundation/NSProcessInfo.hpp4//5// Copyright 2020-2024 Apple Inc.6//7// Licensed under the Apache License, Version 2.0 (the "License");8// you may not use this file except in compliance with the License.9// You may obtain a copy of the License at10//11//     http://www.apache.org/licenses/LICENSE-2.012//13// Unless required by applicable law or agreed to in writing, software14// distributed under the License is distributed on an "AS IS" BASIS,15// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.16// See the License for the specific language governing permissions and17// limitations under the License.18//19//-------------------------------------------------------------------------------------------------------------------------------------------------------------2021#pragma once2223//-------------------------------------------------------------------------------------------------------------------------------------------------------------2425#include "NSDefines.hpp"26#include "NSNotification.hpp"27#include "NSObject.hpp"28#include "NSPrivate.hpp"29#include "NSTypes.hpp"3031#include <functional>3233//-------------------------------------------------------------------------------------------------------------------------------------------------------------3435namespace NS36{37_NS_CONST(NotificationName, ProcessInfoThermalStateDidChangeNotification);38_NS_CONST(NotificationName, ProcessInfoPowerStateDidChangeNotification);39_NS_CONST(NotificationName, ProcessInfoPerformanceProfileDidChangeNotification);4041_NS_ENUM(NS::Integer, ProcessInfoThermalState) {42    ProcessInfoThermalStateNominal = 0,43    ProcessInfoThermalStateFair = 1,44    ProcessInfoThermalStateSerious = 2,45    ProcessInfoThermalStateCritical = 346};4748_NS_OPTIONS(std::uint64_t, ActivityOptions) {49    ActivityIdleDisplaySleepDisabled = (1ULL << 40),50    ActivityIdleSystemSleepDisabled = (1ULL << 20),51    ActivitySuddenTerminationDisabled = (1ULL << 14),52    ActivityAutomaticTerminationDisabled = (1ULL << 15),53    ActivityUserInitiated = (0x00FFFFFFULL | ActivityIdleSystemSleepDisabled),54    ActivityUserInitiatedAllowingIdleSystemSleep = (ActivityUserInitiated & ~ActivityIdleSystemSleepDisabled),55    ActivityBackground = 0x000000FFULL,56    ActivityLatencyCritical = 0xFF00000000ULL,57};5859typedef NS::Integer DeviceCertification;60_NS_CONST(DeviceCertification, DeviceCertificationiPhonePerformanceGaming);6162typedef NS::Integer ProcessPerformanceProfile;63_NS_CONST(ProcessPerformanceProfile, ProcessPerformanceProfileDefault);64_NS_CONST(ProcessPerformanceProfile, ProcessPerformanceProfileSustained);6566class ProcessInfo : public Referencing<ProcessInfo>67{68public:69    static ProcessInfo*     processInfo();7071    class Array*            arguments() const;72    class Dictionary*       environment() const;73    class String*           hostName() const;74    class String*           processName() const;75    void                    setProcessName(const String* pString);76    int                     processIdentifier() const;77    class String*           globallyUniqueString() const;7879    class String*           userName() const;80    class String*           fullUserName() const;8182    UInteger                operatingSystem() const;83    OperatingSystemVersion  operatingSystemVersion() const;84    class String*           operatingSystemVersionString() const;85    bool                    isOperatingSystemAtLeastVersion(OperatingSystemVersion version) const;8687    UInteger                processorCount() const;88    UInteger                activeProcessorCount() const;89    unsigned long long      physicalMemory() const;90    TimeInterval            systemUptime() const;9192    void                    disableSuddenTermination();93    void                    enableSuddenTermination();9495    void                    disableAutomaticTermination(const class String* pReason);96    void                    enableAutomaticTermination(const class String* pReason);97    bool                    automaticTerminationSupportEnabled() const;98    void                    setAutomaticTerminationSupportEnabled(bool enabled);99100    class Object*           beginActivity(ActivityOptions options, const class String* pReason);101    void                    endActivity(class Object* pActivity);102    void                    performActivity(ActivityOptions options, const class String* pReason, void (^block)(void));103    void                    performActivity(ActivityOptions options, const class String* pReason, const std::function<void()>& func);104    void                    performExpiringActivity(const class String* pReason, void (^block)(bool expired));105    void                    performExpiringActivity(const class String* pReason, const std::function<void(bool expired)>& func);106107    ProcessInfoThermalState thermalState() const;108    bool                    isLowPowerModeEnabled() const;109110    bool                    isiOSAppOnMac() const;111    bool                    isMacCatalystApp() const;112113    bool                    isDeviceCertified(DeviceCertification performanceTier) const;114    bool                    hasPerformanceProfile(ProcessPerformanceProfile performanceProfile) const;115116};117}118119//-------------------------------------------------------------------------------------------------------------------------------------------------------------120121_NS_PRIVATE_DEF_CONST(NS::NotificationName, ProcessInfoThermalStateDidChangeNotification);122_NS_PRIVATE_DEF_CONST(NS::NotificationName, ProcessInfoPowerStateDidChangeNotification);123124// The linker searches for these symbols in the Metal framework, be sure to link it in as well:125_NS_PRIVATE_DEF_CONST(NS::NotificationName, ProcessInfoPerformanceProfileDidChangeNotification);126_NS_PRIVATE_DEF_CONST(NS::DeviceCertification, DeviceCertificationiPhonePerformanceGaming);127_NS_PRIVATE_DEF_CONST(NS::ProcessPerformanceProfile, ProcessPerformanceProfileDefault);128_NS_PRIVATE_DEF_CONST(NS::ProcessPerformanceProfile, ProcessPerformanceProfileSustained);129130//-------------------------------------------------------------------------------------------------------------------------------------------------------------131132_NS_INLINE NS::ProcessInfo* NS::ProcessInfo::processInfo()133{134    return Object::sendMessage<ProcessInfo*>(_NS_PRIVATE_CLS(NSProcessInfo), _NS_PRIVATE_SEL(processInfo));135}136137//-------------------------------------------------------------------------------------------------------------------------------------------------------------138139_NS_INLINE NS::Array* NS::ProcessInfo::arguments() const140{141    return Object::sendMessage<Array*>(this, _NS_PRIVATE_SEL(arguments));142}143144//-------------------------------------------------------------------------------------------------------------------------------------------------------------145146_NS_INLINE NS::Dictionary* NS::ProcessInfo::environment() const147{148    return Object::sendMessage<Dictionary*>(this, _NS_PRIVATE_SEL(environment));149}150151//-------------------------------------------------------------------------------------------------------------------------------------------------------------152153_NS_INLINE NS::String* NS::ProcessInfo::hostName() const154{155    return Object::sendMessage<String*>(this, _NS_PRIVATE_SEL(hostName));156}157158//-------------------------------------------------------------------------------------------------------------------------------------------------------------159160_NS_INLINE NS::String* NS::ProcessInfo::processName() const161{162    return Object::sendMessage<String*>(this, _NS_PRIVATE_SEL(processName));163}164165//-------------------------------------------------------------------------------------------------------------------------------------------------------------166167_NS_INLINE void NS::ProcessInfo::setProcessName(const String* pString)168{169    Object::sendMessage<void>(this, _NS_PRIVATE_SEL(setProcessName_), pString);170}171172//-------------------------------------------------------------------------------------------------------------------------------------------------------------173174_NS_INLINE int NS::ProcessInfo::processIdentifier() const175{176    return Object::sendMessage<int>(this, _NS_PRIVATE_SEL(processIdentifier));177}178179//-------------------------------------------------------------------------------------------------------------------------------------------------------------180181_NS_INLINE NS::String* NS::ProcessInfo::globallyUniqueString() const182{183    return Object::sendMessage<String*>(this, _NS_PRIVATE_SEL(globallyUniqueString));184}185186//-------------------------------------------------------------------------------------------------------------------------------------------------------------187188_NS_INLINE NS::String* NS::ProcessInfo::userName() const189{190    return Object::sendMessageSafe<String*>(this, _NS_PRIVATE_SEL(userName));191}192193//-------------------------------------------------------------------------------------------------------------------------------------------------------------194195_NS_INLINE NS::String* NS::ProcessInfo::fullUserName() const196{197    return Object::sendMessageSafe<String*>(this, _NS_PRIVATE_SEL(fullUserName));198}199200//-------------------------------------------------------------------------------------------------------------------------------------------------------------201202_NS_INLINE NS::UInteger NS::ProcessInfo::operatingSystem() const203{204    return Object::sendMessage<UInteger>(this, _NS_PRIVATE_SEL(operatingSystem));205}206207//-------------------------------------------------------------------------------------------------------------------------------------------------------------208209_NS_INLINE NS::OperatingSystemVersion NS::ProcessInfo::operatingSystemVersion() const210{211    return Object::sendMessage<OperatingSystemVersion>(this, _NS_PRIVATE_SEL(operatingSystemVersion));212}213214//-------------------------------------------------------------------------------------------------------------------------------------------------------------215216_NS_INLINE NS::String* NS::ProcessInfo::operatingSystemVersionString() const217{218    return Object::sendMessage<String*>(this, _NS_PRIVATE_SEL(operatingSystemVersionString));219}220221//-------------------------------------------------------------------------------------------------------------------------------------------------------------222223_NS_INLINE bool NS::ProcessInfo::isOperatingSystemAtLeastVersion(OperatingSystemVersion version) const224{225    return Object::sendMessage<bool>(this, _NS_PRIVATE_SEL(isOperatingSystemAtLeastVersion_), version);226}227228//-------------------------------------------------------------------------------------------------------------------------------------------------------------229230_NS_INLINE NS::UInteger NS::ProcessInfo::processorCount() const231{232    return Object::sendMessage<UInteger>(this, _NS_PRIVATE_SEL(processorCount));233}234235//-------------------------------------------------------------------------------------------------------------------------------------------------------------236237_NS_INLINE NS::UInteger NS::ProcessInfo::activeProcessorCount() const238{239    return Object::sendMessage<UInteger>(this, _NS_PRIVATE_SEL(activeProcessorCount));240}241242//-------------------------------------------------------------------------------------------------------------------------------------------------------------243244_NS_INLINE unsigned long long NS::ProcessInfo::physicalMemory() const245{246    return Object::sendMessage<unsigned long long>(this, _NS_PRIVATE_SEL(physicalMemory));247}248249//-------------------------------------------------------------------------------------------------------------------------------------------------------------250251_NS_INLINE NS::TimeInterval NS::ProcessInfo::systemUptime() const252{253    return Object::sendMessage<TimeInterval>(this, _NS_PRIVATE_SEL(systemUptime));254}255256//-------------------------------------------------------------------------------------------------------------------------------------------------------------257258_NS_INLINE void NS::ProcessInfo::disableSuddenTermination()259{260    Object::sendMessageSafe<void>(this, _NS_PRIVATE_SEL(disableSuddenTermination));261}262263//-------------------------------------------------------------------------------------------------------------------------------------------------------------264265_NS_INLINE void NS::ProcessInfo::enableSuddenTermination()266{267    Object::sendMessageSafe<void>(this, _NS_PRIVATE_SEL(enableSuddenTermination));268}269270//-------------------------------------------------------------------------------------------------------------------------------------------------------------271272_NS_INLINE void NS::ProcessInfo::disableAutomaticTermination(const String* pReason)273{274    Object::sendMessageSafe<void>(this, _NS_PRIVATE_SEL(disableAutomaticTermination_), pReason);275}276277//-------------------------------------------------------------------------------------------------------------------------------------------------------------278279_NS_INLINE void NS::ProcessInfo::enableAutomaticTermination(const String* pReason)280{281    Object::sendMessageSafe<void>(this, _NS_PRIVATE_SEL(enableAutomaticTermination_), pReason);282}283284//-------------------------------------------------------------------------------------------------------------------------------------------------------------285286_NS_INLINE bool NS::ProcessInfo::automaticTerminationSupportEnabled() const287{288    return Object::sendMessageSafe<bool>(this, _NS_PRIVATE_SEL(automaticTerminationSupportEnabled));289}290291//-------------------------------------------------------------------------------------------------------------------------------------------------------------292293_NS_INLINE void NS::ProcessInfo::setAutomaticTerminationSupportEnabled(bool enabled)294{295    Object::sendMessageSafe<void>(this, _NS_PRIVATE_SEL(setAutomaticTerminationSupportEnabled_), enabled);296}297298//-------------------------------------------------------------------------------------------------------------------------------------------------------------299300_NS_INLINE NS::Object* NS::ProcessInfo::beginActivity(ActivityOptions options, const String* pReason)301{302    return Object::sendMessage<Object*>(this, _NS_PRIVATE_SEL(beginActivityWithOptions_reason_), options, pReason);303}304305//-------------------------------------------------------------------------------------------------------------------------------------------------------------306307_NS_INLINE void NS::ProcessInfo::endActivity(Object* pActivity)308{309    Object::sendMessage<void>(this, _NS_PRIVATE_SEL(endActivity_), pActivity);310}311312//-------------------------------------------------------------------------------------------------------------------------------------------------------------313314_NS_INLINE void NS::ProcessInfo::performActivity(ActivityOptions options, const String* pReason, void (^block)(void))315{316    Object::sendMessage<void>(this, _NS_PRIVATE_SEL(performActivityWithOptions_reason_usingBlock_), options, pReason, block);317}318319//-------------------------------------------------------------------------------------------------------------------------------------------------------------320321_NS_INLINE void NS::ProcessInfo::performActivity(ActivityOptions options, const String* pReason, const std::function<void()>& function)322{323    __block std::function<void()> blockFunction = function;324325    performActivity(options, pReason, ^() { blockFunction(); });326}327328//-------------------------------------------------------------------------------------------------------------------------------------------------------------329330_NS_INLINE void NS::ProcessInfo::performExpiringActivity(const String* pReason, void (^block)(bool expired))331{332    Object::sendMessageSafe<void>(this, _NS_PRIVATE_SEL(performExpiringActivityWithReason_usingBlock_), pReason, block);333}334335//-------------------------------------------------------------------------------------------------------------------------------------------------------------336337_NS_INLINE void NS::ProcessInfo::performExpiringActivity(const String* pReason, const std::function<void(bool expired)>& function)338{339    __block std::function<void(bool expired)> blockFunction = function;340341    performExpiringActivity(pReason, ^(bool expired) { blockFunction(expired); });342}343344//-------------------------------------------------------------------------------------------------------------------------------------------------------------345346_NS_INLINE NS::ProcessInfoThermalState NS::ProcessInfo::thermalState() const347{348    return Object::sendMessage<ProcessInfoThermalState>(this, _NS_PRIVATE_SEL(thermalState));349}350351//-------------------------------------------------------------------------------------------------------------------------------------------------------------352353_NS_INLINE bool NS::ProcessInfo::isLowPowerModeEnabled() const354{355    return Object::sendMessageSafe<bool>(this, _NS_PRIVATE_SEL(isLowPowerModeEnabled));356}357358//-------------------------------------------------------------------------------------------------------------------------------------------------------------359360_NS_INLINE bool NS::ProcessInfo::isiOSAppOnMac() const361{362    return Object::sendMessageSafe<bool>(this, _NS_PRIVATE_SEL(isiOSAppOnMac));363}364365//-------------------------------------------------------------------------------------------------------------------------------------------------------------366367_NS_INLINE bool NS::ProcessInfo::isMacCatalystApp() const368{369    return Object::sendMessageSafe<bool>(this, _NS_PRIVATE_SEL(isMacCatalystApp));370}371372//-------------------------------------------------------------------------------------------------------------------------------------------------------------373374_NS_INLINE bool NS::ProcessInfo::isDeviceCertified(DeviceCertification performanceTier) const375{376    return Object::sendMessageSafe<bool>(this, _NS_PRIVATE_SEL(isDeviceCertified_), performanceTier);377}378379//-------------------------------------------------------------------------------------------------------------------------------------------------------------380381_NS_INLINE bool NS::ProcessInfo::hasPerformanceProfile(ProcessPerformanceProfile performanceProfile) const382{383    return Object::sendMessageSafe<bool>(this, _NS_PRIVATE_SEL(hasPerformanceProfile_), performanceProfile);384}385386//-------------------------------------------------------------------------------------------------------------------------------------------------------------387