diff options
| author | ruki <[email protected]> | 2022-07-29 22:36:04 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-07-29 22:36:04 +0800 |
| commit | 62a97765a4e28a6437d011e2f9dd09d538c1e62c (patch) | |
| tree | d29dbc173f568c4bff2f46b6751afd73f5e89a6a | |
| parent | c7d8940dbe8c82b5690c60136a251c30e6214c66 (diff) | |
get cpuinfo for linux
| -rw-r--r-- | core/src/xmake/os/cpuinfo.c | 57 | ||||
| -rw-r--r-- | core/src/xmake/os/meminfo.c | 1 | ||||
| -rw-r--r-- | xmake/modules/private/action/build/object.lua | 1 |
3 files changed, 59 insertions, 0 deletions
diff --git a/core/src/xmake/os/cpuinfo.c b/core/src/xmake/os/cpuinfo.c index 473db2fdc..a36ec43cd 100644 --- a/core/src/xmake/os/cpuinfo.c +++ b/core/src/xmake/os/cpuinfo.c @@ -37,6 +37,8 @@ # include <mach/mach_host.h> #elif defined(TB_CONFIG_OS_WINDOWS) # include <windows.h> +#elif defined(TB_CONFIG_OS_LINUX) +# include <stdio.h> #endif /* ////////////////////////////////////////////////////////////////////////////////////// @@ -122,6 +124,61 @@ static tb_float_t xm_os_cpuinfo_usagerate() user_prev = user; } return usagerate; +#elif defined(TB_CONFIG_OS_LINUX) + tb_float_t usagerate = 0; + if (tb_file_info("/proc/stat", tb_null)) + { + tb_bool_t ok = tb_false; + FILE* fp = fopen("/proc/stat", "r"); + if (fp) + { + tb_char_t line[8192]; + static tb_int64_t total_prev = 0; + static tb_int64_t active_prev = 0; + while (!feof(fp)) + { + /* cpu 548760 0 867417 102226682 12430 0 9089 0 0 0 + * cpu0 136863 0 218110 25632388 2706 0 2328 0 0 0 + * cpu1 148383 0 213941 25627686 3925 0 2129 0 0 0 + * + * The meanings of the columns are as follows, from left to right: + * + * user: normal processes executing in user mode + * nice: niced processes executing in user mode + * system: processes executing in kernel mode + * idle: twiddling thumbs + * iowait: waiting for I/O to complete + * irq: servicing interrupts + * softirq: servicing softirqs + * steal + * guest + * guest_nice + */ + if (fgets(line, sizeof(line), fp) && !tb_strncmp(line, "cpu ", 4)) + { + tb_int64_t user, nice, sys, idle, iowait, irq, softirq, steal, guest, guest_nice; + if (10 == sscanf(line, "cpu %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld", + &user, &nice, &sys, &idle, &iowait, &irq, &softirq, &steal, &guest, &guest_nice)) + { + tb_int64_t active = user + nice + sys + irq + softirq + steal + guest + guest_nice; + tb_int64_t total = user + nice + sys + idle + iowait + irq + softirq + steal + guest + guest_nice; + if (total_prev > 0 && active_prev > 0) + { + tb_int64_t total_diff = total - total_prev; + tb_int64_t active_diff = active - active_prev; + if (total_diff > 0) + usagerate = (tb_float_t)((tb_double_t)active_diff / total_diff); + } + total_prev = total; + active_prev = active; + } + break; + } + } + fclose(fp); + } + } + return usagerate; #else return 0; #endif diff --git a/core/src/xmake/os/meminfo.c b/core/src/xmake/os/meminfo.c index b679e5aee..cd9ace234 100644 --- a/core/src/xmake/os/meminfo.c +++ b/core/src/xmake/os/meminfo.c @@ -36,6 +36,7 @@ # include <mach-o/dyld.h> # include <mach-o/nlist.h> #elif defined(TB_CONFIG_OS_LINUX) +# include <stdio.h> # include <sys/sysinfo.h> #elif defined(TB_CONFIG_OS_WINDOWS) # include <windows.h> diff --git a/xmake/modules/private/action/build/object.lua b/xmake/modules/private/action/build/object.lua index c906ea8de..70446949a 100644 --- a/xmake/modules/private/action/build/object.lua +++ b/xmake/modules/private/action/build/object.lua @@ -48,6 +48,7 @@ function _do_build_file(target, sourcefile, opt) -- dry run? local dryrun = option.get("dry-run") + print(os.cpuinfo("usagerate")) -- need build this object? -- -- we need use `os.mtime(dependfile)` to determine the mtime of the dependfile to avoid objectfile corruption due to compilation interruptions |
