PIInit refactoring, iteration 2

add to PISystemInfo processorsCount<P|E> fields, now counts only for Windows
Introspection public API works without PIP_HAS_THREADS
This commit is contained in:
2026-09-06 10:41:53 +03:00
parent 7051e138e9
commit 3da94abb2a
7 changed files with 244 additions and 196 deletions
+3 -2
View File
@@ -419,8 +419,9 @@ if(WIN32)
endif()
# setupapi/hid: HID device enumeration (PIHIDevice)
if(PIP_HAS_THREADS AND PIP_HAS_FILESYSTEM)
list(APPEND LIBS_MAIN cfgmgr32 setupapi hid)
list(APPEND LIBS_MAIN cfgmgr32 hid)
endif()
list(APPEND LIBS_MAIN setupapi)
endif()
else()
if(PIP_HAS_DYNLIB)
@@ -740,7 +741,7 @@ else()
endif()
# Auxiliary
if (_PIP_HOST_TOOLS AND (NOT CROSSTOOLS))
if (_PIP_HOST_TOOLS AND (NOT CROSSTOOLS) AND PIP_BUILD_CONSOLE)
add_subdirectory("utils/piterminal")
endif()
+202 -187
View File
@@ -22,7 +22,7 @@
#include "piincludes_p.h"
// The full implementation needs the PIInit class (declared in piinit.h only
// with PIP_HAS_THREADS); without threads PIInit is absent altogether
#if !defined(_PIP_INIT_STUB_) && defined(PIP_HAS_THREADS)
#if !defined(_PIP_INIT_STUB_)
# include "pidir.h"
# include "piliterals_string.h"
@@ -36,7 +36,11 @@
# include "esp_system.h"
# endif
# ifdef WINDOWS
# include <winsock2.h>
# ifdef PIP_HAS_SOCKET
# include <winsock2.h>
# else
# include <windows.h>
# endif
extern FILETIME __pi_ftjan1970;
extern PINtQueryTimerResolution getTimerResolutionAddr;
extern PINtSetTimerResolution setTimerResolutionAddr;
@@ -99,117 +103,13 @@ void __sighandler__(PISignals::Signal s) {
}
void PIInit::initSignals() {
# ifndef ANDROID
PISignals::setSlot(__sighandler__);
PISignals::grabSignals(PISignals::UserDefined1);
# ifndef WINDOWS
PISignals::grabSignals(PISignals::StopTTYInput | PISignals::StopTTYOutput);
sigset_t ss;
sigemptyset(&ss);
sigaddset(&ss, SIGALRM);
sigprocmask(SIG_BLOCK, &ss, 0);
pthread_sigmask(SIG_BLOCK, &ss, 0);
signal(SIGPIPE, SIG_IGN);
# endif // WINDOWS
# endif // WINDOWS
}
void PIInit::initLocale() {
# ifdef HAS_LOCALE
// std::cout << "has locale" << std::endl;
if (currentLocale_t != 0) {
freelocale(currentLocale_t);
currentLocale_t = 0;
}
currentLocale_t = newlocale(LC_ALL, setlocale(LC_ALL, "C"), 0);
setlocale(LC_CTYPE, "en_US.UTF-8");
# else // HAS_LOCALE
setlocale(LC_ALL, "");
setlocale(LC_NUMERIC, "C");
# endif // HAS_LOCALE
PRIVATE->delete_locs = false;
__syslocname__ = __sysoemname__ = 0;
__utf8name__ = const_cast<char *>("UTF-8");
# ifdef PIP_ICU
UErrorCode e((UErrorCode)0);
u_init(&e);
# endif // PIP_ICU
}
void PIInit::initNames(PISystemInfo * sinfo) {
char cbuff[1024];
piZeroMemory(cbuff, 1024);
if (gethostname(cbuff, 1023) == 0) {
sinfo->hostname = cbuff;
}
# ifndef WINDOWS
# ifdef ESP_PLATFORM
esp_chip_info_t chip_info;
esp_chip_info(&chip_info);
sinfo->processorsCount = chip_info.cores;
sinfo->architecture = "Xtensa LX6";
// printf("silicon revision %d, ", chip_info.revision);
sinfo->OS_version = esp_get_idf_version();
# else
PIStringList ifpathes;
ifpathes << "/bin/ifconfig"_a << "/sbin/ifconfig"_a << "/usr/bin/ifconfig"_a
<< "/usr/sbin/ifconfig"_a;
for (const auto & i: ifpathes) {
if (fileExists(i)) {
sinfo->ifconfigPath = i;
break;
}
}
sinfo->processorsCount = piMaxi(1, int(sysconf(_SC_NPROCESSORS_ONLN)));
passwd * ps = getpwuid(getuid());
if (ps)
sinfo->user = ps->pw_name;
else {
piZeroMemory(cbuff, 1024);
char * l = getlogin();
if (l) sinfo->user = l;
}
struct utsname uns;
if (uname(&uns) == 0) {
sinfo->OS_version = uns.release;
sinfo->architecture = uns.machine;
# ifdef LINUX
sinfo->OS_name = uns.sysname;
# endif
}
# endif
# endif // WINDOWS
}
void PIInit::initFinalize(PISystemInfo * sinfo) {
if (sinfo->processorsCountP + sinfo->processorsCountE != sinfo->processorsCount) {
sinfo->processorsCountE = 0;
sinfo->processorsCountP = sinfo->processorsCount;
}
}
void PIInit::initPlatform() {
# ifdef WINDOWS
void PIInit::initPlatform(PISystemInfo * sinfo) {
sinfo->OS_name = "Windows"_a;
initSignals();
initNames(sinfo);
// OS version
DWORD dwVersion = GetVersion();
DWORD dwMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
DWORD dwMinorVersion = (DWORD)(HIBYTE(LOWORD(dwVersion)));
sinfo->OS_version = PIString::fromNumber(dwMajorVersion) + "." + PIString::fromNumber(dwMinorVersion);
# ifdef PIP_HAS_SOCKET
// WinSock inint
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
# endif
// Timers init
SYSTEMTIME jan1970 = {1970, 1, 4, 1, 0, 0, 0, 0};
@@ -226,6 +126,34 @@ void PIInit::initPlatform(PISystemInfo * sinfo) {
setTimerResolutionAddr = (PINtSetTimerResolution)GetProcAddress(PRIVATE->ntlib, "NtSetTimerResolution");
__PISetTimerResolution();
}
# endif
# ifdef MAC_OS
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &__pi_mac_clock);
# endif
}
void PIInit::initSignals() {
# ifndef ANDROID
PISignals::setSlot(__sighandler__);
PISignals::grabSignals(PISignals::UserDefined1);
# ifndef WINDOWS
PISignals::grabSignals(PISignals::StopTTYInput | PISignals::StopTTYOutput);
sigset_t ss;
sigemptyset(&ss);
sigaddset(&ss, SIGALRM);
sigprocmask(SIG_BLOCK, &ss, 0);
pthread_sigmask(SIG_BLOCK, &ss, 0);
signal(SIGPIPE, SIG_IGN);
# endif // WINDOWS
# endif // ANDROID
}
void PIInit::initLocale() {
# ifdef WINDOWS
// Windows branch
# ifdef PIP_ICU
UErrorCode e((UErrorCode)0);
@@ -246,6 +174,128 @@ void PIInit::initPlatform(PISystemInfo * sinfo) {
__utf8name__ = (char *)CP_UTF8;
# endif
# else // WINDOWS
// Non-Windows branch
# ifdef HAS_LOCALE
// std::cout << "has locale" << std::endl;
if (currentLocale_t != 0) {
freelocale(currentLocale_t);
currentLocale_t = 0;
}
currentLocale_t = newlocale(LC_ALL, setlocale(LC_ALL, "C"), 0);
setlocale(LC_CTYPE, "en_US.UTF-8");
# else // HAS_LOCALE
setlocale(LC_ALL, "");
setlocale(LC_NUMERIC, "C");
# endif // HAS_LOCALE
PRIVATE->delete_locs = false;
__syslocname__ = __sysoemname__ = 0;
__utf8name__ = const_cast<char *>("UTF-8");
# ifdef PIP_ICU
UErrorCode e((UErrorCode)0);
u_init(&e);
# endif // PIP_ICU
# endif // WINDOWS
}
void PIInit::initPaths() {
# ifdef WINDOWS
// execute command
int argc_(0);
wchar_t ** argv_ = CommandLineToArgvW(GetCommandLineW(), &argc_);
if (argc_ > 0 && argv_ != 0) sinfo->execCommand = argv_[0];
LocalFree(argv_);
# else
# ifndef PIP_EMBEDDED
PIStringList ifpathes;
ifpathes << "/bin/ifconfig"_a << "/sbin/ifconfig"_a << "/usr/bin/ifconfig"_a
<< "/usr/sbin/ifconfig"_a;
for (const auto & i: ifpathes) {
if (fileExists(i)) {
sinfo->ifconfigPath = i;
break;
}
}
# endif // PIP_EMBEDDED
# endif // WINDOWS
}
void PIInit::initHostname() {
# ifdef PIP_HAS_SOCKET
char cbuff[1024];
piZeroMemory(cbuff, 1024);
if (gethostname(cbuff, 1023) == 0) {
sinfo->hostname = cbuff;
}
# endif
}
void PIInit::initUsername() {
# ifdef WINDOWS
char cbuff[1024];
piZeroMemory(cbuff, 1024);
ulong unlen = 1023;
if (GetUserNameA(cbuff, &unlen) != 0) sinfo->user = cbuff;
# else
# ifndef ESP_PLATFORM
passwd * ps = getpwuid(getuid());
if (ps)
sinfo->user = ps->pw_name;
else {
char cbuff[1024];
piZeroMemory(cbuff, 1024);
char * l = getlogin();
if (l) sinfo->user = l;
}
# endif // WINDOWS
# endif // WINDOWS
}
void PIInit::initOSname() {
# ifndef LINUX
sinfo->OS_name =
# ifdef WINDOWS
"Windows"_a;
# elif defined(QNX)
"QNX"_a;
# elif defined(MAC_OS)
"MacOS"_a;
# elif defined(ANDROID)
"Android"_a;
# elif defined(FREE_BSD)
"FreeBSD"_a;
# elif defined(FREERTOS)
"FreeRTOS"_a;
# else
"Unknown"_a;
# endif
# endif
}
void PIInit::initHWInfo() {
# ifdef WINDOWS
// OS version
DWORD dwVersion = GetVersion();
DWORD dwMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
DWORD dwMinorVersion = (DWORD)(HIBYTE(LOWORD(dwVersion)));
sinfo->OS_version = PIString::fromNumber(dwMajorVersion) + "." + PIString::fromNumber(dwMinorVersion);
// architecture
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
@@ -285,98 +335,63 @@ void PIInit::initPlatform(PISystemInfo * sinfo) {
printf("GetLogicalProcessorInformationEx error\n");
}
// execute command
int argc_(0);
wchar_t ** argv_ = CommandLineToArgvW(GetCommandLineW(), &argc_);
if (argc_ > 0 && argv_ != 0) sinfo->execCommand = argv_[0];
LocalFree(argv_);
# else
// username
char cbuff[1024];
piZeroMemory(cbuff, 1024);
ulong unlen = 1023;
if (GetUserNameA(cbuff, &unlen) != 0) sinfo->user = cbuff;
}
# endif
# ifdef ESP_PLATFORM
esp_chip_info_t chip_info;
esp_chip_info(&chip_info);
sinfo->processorsCount = chip_info.cores;
sinfo->architecture = "Xtensa LX6";
// printf("silicon revision %d, ", chip_info.revision);
sinfo->OS_version = esp_get_idf_version();
# else
sinfo->processorsCount = piMaxi(1, int(sysconf(_SC_NPROCESSORS_ONLN)));
# ifdef QNX
void PIInit::initPlatform(PISystemInfo * sinfo) {
sinfo->OS_name = "QNX"_a;
initSignals();
initLocale();
initNames(sinfo);
}
# endif
struct utsname uns;
if (uname(&uns) == 0) {
sinfo->OS_version = uns.release;
sinfo->architecture = uns.machine;
# ifdef LINUX
sinfo->OS_name = uns.sysname;
# endif
}
# endif
# ifdef MAC_OS
void PIInit::initPlatform(PISystemInfo * sinfo) {
sinfo->OS_name = "MacOS"_a;
initSignals();
initLocale();
initNames(sinfo);
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &__pi_mac_clock);
}
# endif
# ifdef ANDROID
void PIInit::initPlatform(PISystemInfo * sinfo) {
sinfo->OS_name = "Android"_a;
initNames(sinfo);
// force utf-8
PRIVATE->delete_locs = false;
__syslocname__ = __sysoemname__ = 0;
__utf8name__ = const_cast<char *>("UTF-8");
}
# endif
# ifdef FREE_BSD
void PIInit::initPlatform(PISystemInfo * sinfo) {
sinfo->OS_name = "FreeBSD"_a;
initSignals();
initLocale();
initNames(sinfo);
# endif // WINDOWS
}
# endif
# ifdef FREERTOS
void PIInit::initPlatform(PISystemInfo * sinfo) {
sinfo->OS_name = "FreeRTOS"_a;
initSignals();
initLocale();
initNames(sinfo);
void PIInit::initFinalize() {
if (sinfo->processorsCountP + sinfo->processorsCountE != sinfo->processorsCount) {
sinfo->processorsCountE = 0;
sinfo->processorsCountP = sinfo->processorsCount;
}
}
# endif
# ifdef LINUX
void PIInit::initPlatform(PISystemInfo * sinfo) {
initSignals();
initLocale();
initNames(sinfo);
}
# endif
PIInit::PIInit() {
PISystemInfo * sinfo = PISystemInfo::instance();
sinfo->execDateTime = PIDateTime::current();
initPlatform(sinfo);
initFinalize(sinfo);
sinfo = PISystemInfo::instance();
sinfo->execDateTime = PIDateTime::current();
initPlatform();
initSignals();
initLocale();
initPaths();
initHostname();
initUsername();
initOSname();
initHWInfo();
initFinalize();
}
PIInit::~PIInit() {
PIResourcesStorage::instance()->clear();
# ifdef WINDOWS
# ifdef PIP_HAS_SOCKET
WSACleanup();
# endif
if (PRIVATE->ntlib) FreeLibrary(PRIVATE->ntlib);
PRIVATE->ntlib = nullptr;
# endif
@@ -510,9 +525,9 @@ __PIInit_Initializer__::~__PIInit_Initializer__() {
}
#else
# ifdef PIP_HAS_THREADS
# include "pistring.h"
# include "pisysteminfo.h"
# include "pistring.h"
# include "pisysteminfo.h"
// stub: PIInit is never instantiated, __instance__ stays null
int __PIInit_Initializer__::count_(0);
PIInit * __PIInit_Initializer__::__instance__(0);
@@ -533,5 +548,5 @@ __PIInit_Initializer__::~__PIInit_Initializer__() {
__instance__ = nullptr;
}
}
# endif // PIP_HAS_THREADS
#endif // _PIP_INIT_STUB_
#endif // _PIP_INIT_STUB_
+11 -7
View File
@@ -36,9 +36,7 @@
# define _PIP_INIT_STUB_
#endif
#ifdef PIP_HAS_THREADS
# include "piincludes.h"
#include "piincludes.h"
class PIFile;
@@ -99,15 +97,21 @@ public:
private:
explicit PIInit();
void initPlatform(PISystemInfo * sinfo);
void initPlatform();
void initSignals();
void initLocale();
void initNames(PISystemInfo * sinfo);
void initFinalize(PISystemInfo * sinfo);
void initPaths();
void initHostname();
void initUsername();
void initOSname();
void initHWInfo();
void initFinalize();
bool fileExists(const PIString & p);
PRIVATE_DECLARATION(PIP_EXPORT)
PISystemInfo * sinfo = nullptr;
};
#endif // PIP_HAS_THREADS
#endif // PIINIT_H
@@ -90,6 +90,7 @@ void PIIntrospection::unpackInfo(PIByteArray & ba, PIIntrospection::ProcessInfo
}
#ifdef PIP_HAS_THREADS
PIByteArray PIIntrospection::packProcStat(PISystemMonitor * sm) {
ProcessStat ps;
if (sm) {
@@ -105,6 +106,7 @@ PIByteArray PIIntrospection::packProcStat(PISystemMonitor * sm) {
void PIIntrospection::unpackProcStat(PIByteArray & ba, PIIntrospection::ProcessStat & info) {
ba >> info.proc >> info.threads;
}
#endif
PIByteArray PIIntrospection::packContainers() {
@@ -135,6 +137,7 @@ PIByteArray PIIntrospection::packThreads() {
p = PIINTROSPECTION_THREADS->p;
#endif
if (p) {
#ifdef PIP_HAS_THREADS
p->mutex.lock();
PIMap<PIThread *, PIIntrospectionThreads::ThreadInfo> & tm(p->threads);
auto it = tm.makeIterator();
@@ -148,6 +151,7 @@ PIByteArray PIIntrospection::packThreads() {
p->mutex.unlock();
} else {
ret << PIVector<PIIntrospectionThreads::ThreadInfo>();
#endif
}
return ret;
}
@@ -56,8 +56,10 @@ public:
struct PIP_EXPORT ProcessStat {
ProcessStat() {}
#ifdef PIP_HAS_THREADS
PISystemMonitor::ProcessStats proc;
PIVector<PISystemMonitor::ThreadStats> threads;
#endif
};
struct PIP_EXPORT ObjectInfo {
@@ -77,8 +79,10 @@ public:
static PIByteArray packInfo();
static void unpackInfo(PIByteArray & ba, ProcessInfo & info);
#ifdef PIP_HAS_THREADS
static PIByteArray packProcStat(PISystemMonitor * sm);
static void unpackProcStat(PIByteArray & ba, ProcessStat & info);
#endif
static PIByteArray packContainers();
static void unpackContainers(PIByteArray & ba, PIVector<PIIntrospectionContainers::TypeInfo> & data);
@@ -32,49 +32,63 @@ PIIntrospectionThreads::PIIntrospectionThreads() {}
void PIIntrospectionThreads::threadNew(PIThread * t) {
#ifdef PIP_HAS_THREADS
PIMutexLocker _ml(mutex);
threads.insert(t, ThreadInfo());
#endif
}
void PIIntrospectionThreads::threadDelete(PIThread * t) {
#ifdef PIP_HAS_THREADS
PIMutexLocker _ml(mutex);
threads.remove(t);
#endif
}
void PIIntrospectionThreads::threadStart(PIThread * t) {
#ifdef PIP_HAS_THREADS
PIMutexLocker _ml(mutex);
ThreadInfo & ti(threads[t]);
ti.id = t->tid();
ti.priority = t->priority();
ti.delay = t->delay_.toMilliseconds();
ti.state = sStarting;
#endif
}
void PIIntrospectionThreads::threadRun(PIThread * t) {
#ifdef PIP_HAS_THREADS
PIMutexLocker _ml(mutex);
ThreadInfo & ti(threads[t]);
ti.state = sRunning;
ti.run_count++;
#endif
}
void PIIntrospectionThreads::threadWait(PIThread * t) {
#ifdef PIP_HAS_THREADS
PIMutexLocker _ml(mutex);
threads[t].state = sWaiting;
#endif
}
void PIIntrospectionThreads::threadStop(PIThread * t) {
#ifdef PIP_HAS_THREADS
PIMutexLocker _ml(mutex);
threads[t].state = sStopped;
#endif
}
void PIIntrospectionThreads::threadRunDone(PIThread * t, ullong us) {
#ifdef PIP_HAS_THREADS
PIMutexLocker _ml(mutex);
ThreadInfo & ti(threads[t]);
ti.run_us = (ti.run_us * 0.8) + (us * 0.2); /// WARNING
#endif
}
+6
View File
@@ -111,7 +111,13 @@ public:
//! \~english System logical processors count.
//! \~russian Количество логических процессоров системы.
int processorsCount = 1;
//! \~english System performance logical processors count.
//! \~russian Количество производительных логических процессоров системы.
int processorsCountP = 1;
//! \~english System efficient logical processors count.
//! \~russian Количество эффективных логических процессоров системы.
int processorsCountE = 0;
//! \~english Returns all mount points absolute pathes.