ohmu
·
2026-10-15
1#include "common/printing.h"
2#include "common/jsonconfig.h"
3#include "common/percent.h"
4#include "common/strutil.h"
5#include "detection/cpuusage/cpuusage.h"
6#include "modules/cpuusage/cpuusage.h"
7
8bool ffPrintCPUUsage(FFCPUUsageOptions* options) {
9 FF_LIST_AUTO_DESTROY percentages = ffListCreate();
10 const char* error = ffGetCpuUsageResult(options, &percentages);
11
12 if (error) {
13 ffPrintError(FF_MODULE_GET_DISPLAY_NAME(CPUUsage), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
14 return false;
15 }
16
17 double maxValue = -999, minValue = 999, sumValue = 0;
18 uint32_t maxIndex = 999, minIndex = 999;
19
20 uint32_t index = 0, valueCount = 0;
21 FF_LIST_FOR_EACH (double, percent, percentages) {
22 if (*percent == *percent) {
23 sumValue += *percent;
24
25#if _WIN32
26 // Windows may return values greater than 100%, cap them to 100%
27 if (*percent > 100) {
28 *percent = 100;
29 }
30#endif
31
32 if (*percent > maxValue) {
33 maxValue = *percent;
34 maxIndex = index;
35 }
36 if (*percent < minValue) {
37 minValue = *percent;
38 minIndex = index;
39 }
40 ++valueCount;
41 }
42 ++index;
43 }
44 double avgValue = sumValue / (double) valueCount;
45#if _WIN32
46 // See above comment
47 if (avgValue > 100) {
48 avgValue = 100;
49 }
50#endif
51
52 FFPercentageTypeFlags percentType = options->percent.type == 0 ? instance.config.display.percentType : options->percent.type;
53
54 if (options->moduleArgs.outputFormat.length == 0) {
55 ffPrintLogoAndKey(FF_MODULE_GET_DISPLAY_NAME(CPUUsage), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
56
57 FF_STRBUF_AUTO_DESTROY str = ffStrbufCreate();
58 if (!options->separate) {
59 if (percentType & FF_PERCENTAGE_TYPE_BAR_BIT) {
60 ffPercentAppendBar(&str, avgValue, options->percent, &options->moduleArgs);
61 }
62 if (percentType & FF_PERCENTAGE_TYPE_NUM_BIT) {
63 if (str.length > 0) {
64 ffStrbufAppendC(&str, ' ');
65 }
66 ffPercentAppendNum(&str, avgValue, options->percent, str.length > 0, &options->moduleArgs);
67 }
68 } else {
69 FF_LIST_FOR_EACH (double, percent, percentages) {
70 if (str.length > 0) {
71 ffStrbufAppendC(&str, ' ');
72 }
73 ffPercentAppendNum(&str, *percent, options->percent, false, &options->moduleArgs);
74 }
75 }
76 ffStrbufPutTo(&str, stdout);
77 } else {
78 FF_STRBUF_AUTO_DESTROY avgNum = ffStrbufCreate();
79 if (percentType & FF_PERCENTAGE_TYPE_NUM_BIT) {
80 ffPercentAppendNum(&avgNum, avgValue, options->percent, false, &options->moduleArgs);
81 }
82 FF_STRBUF_AUTO_DESTROY avgBar = ffStrbufCreate();
83 if (percentType & FF_PERCENTAGE_TYPE_BAR_BIT) {
84 ffPercentAppendBar(&avgBar, avgValue, options->percent, &options->moduleArgs);
85 }
86 FF_STRBUF_AUTO_DESTROY minNum = ffStrbufCreate();
87 if (percentType & FF_PERCENTAGE_TYPE_NUM_BIT) {
88 ffPercentAppendNum(&minNum, minValue, options->percent, false, &options->moduleArgs);
89 }
90 FF_STRBUF_AUTO_DESTROY minBar = ffStrbufCreate();
91 if (percentType & FF_PERCENTAGE_TYPE_BAR_BIT) {
92 ffPercentAppendBar(&minBar, minValue, options->percent, &options->moduleArgs);
93 }
94 FF_STRBUF_AUTO_DESTROY maxNum = ffStrbufCreate();
95 if (percentType & FF_PERCENTAGE_TYPE_NUM_BIT) {
96 ffPercentAppendNum(&maxNum, maxValue, options->percent, false, &options->moduleArgs);
97 }
98 FF_STRBUF_AUTO_DESTROY maxBar = ffStrbufCreate();
99 if (percentType & FF_PERCENTAGE_TYPE_BAR_BIT) {
100 ffPercentAppendBar(&maxBar, maxValue, options->percent, &options->moduleArgs);
101 }
102
103 FF_PRINT_FORMAT_CHECKED(FF_MODULE_GET_DISPLAY_NAME(CPUUsage), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
104 FF_ARG(avgNum, "avg"),
105 FF_ARG(maxNum, "max"),
106 FF_ARG(maxIndex, "max-index"),
107 FF_ARG(minNum, "min"),
108 FF_ARG(minIndex, "min-index"),
109 FF_ARG(avgBar, "avg-bar"),
110 FF_ARG(maxBar, "max-bar"),
111 FF_ARG(minBar, "min-bar"),
112 }));
113 }
114
115 return true;
116}
117
118void ffParseCPUUsageJsonObject(FFCPUUsageOptions* options, yyjson_val* module) {
119 yyjson_val *key, *val;
120 size_t idx, max;
121 yyjson_obj_foreach (module, idx, max, key, val) {
122 if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
123 continue;
124 }
125
126 if (unsafe_yyjson_equals_str(key, "separate")) {
127 options->separate = yyjson_get_bool(val);
128 continue;
129 }
130
131 if (unsafe_yyjson_equals_str(key, "waitTime")) {
132 if (!ffJsonConfigParseUInt32(val, &options->waitTime, UINT32_MAX)) {
133 ffPrintError(FF_MODULE_GET_DISPLAY_NAME(CPUUsage), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Property 'waitTime' must be a non-negative integer no greater than 4294967295");
134 continue;
135 }
136 continue;
137 }
138
139 if (ffPercentParseJsonObject(key, val, &options->percent)) {
140 continue;
141 }
142
143 ffPrintError(FF_MODULE_GET_DISPLAY_NAME(CPUUsage), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
144 }
145}
146
147[[gnu::cold]]
148void ffGenerateCPUUsageJsonConfig(FFCPUUsageOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
149 ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
150
151 yyjson_mut_obj_add_bool(doc, module, "separate", options->separate);
152
153 ffPercentGenerateJsonConfig(doc, module, options->percent);
154
155 yyjson_mut_obj_add_uint(doc, module, "waitTime", options->waitTime);
156}
157
158bool ffGenerateCPUUsageJsonResult(FFCPUUsageOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
159 FF_LIST_AUTO_DESTROY percentages = ffListCreate();
160 const char* error = ffGetCpuUsageResult(options, &percentages);
161
162 if (error) {
163 yyjson_mut_obj_add_str(doc, module, "error", error);
164 return false;
165 }
166 yyjson_mut_val* result = yyjson_mut_obj_add_arr(doc, module, "result");
167 FF_LIST_FOR_EACH (double, percent, percentages) {
168 yyjson_mut_arr_add_real(doc, result, *percent);
169 }
170
171 return true;
172}
173
174void ffInitCPUUsageOptions(FFCPUUsageOptions* options) {
175 ffOptionInitModuleArg(&options->moduleArgs, "");
176 options->separate = false;
177 options->percent = (FFPercentageModuleConfig) { 50, 80, 0 };
178 options->waitTime = 200;
179}
180
181void ffDestroyCPUUsageOptions(FFCPUUsageOptions* options) {
182 ffOptionDestroyModuleArg(&options->moduleArgs);
183}
184
185FFModuleBaseInfo ffCPUUsageModuleInfo = {
186 .name = "CPUUsage",
187 .description = "Print CPU usage. Collecting data takes some time",
188 .displayName = {
189 .en = "CPU Usage",
190 .ar = "استخدام المعالج",
191 .cs = "Využití CPU",
192 .de = "CPU-Auslastung",
193 .es = "Uso de la CPU",
194 .fi = "Suorittimen Käyttö",
195 .fr = "Utilisation du CPU",
196 .gl = "Uso da CPU",
197 .he = "שימוש במעבד",
198 .id = "Penggunaan CPU",
199 .it = "Utilizzo della CPU",
200 .ja = "CPU使用率",
201 .ko = "CPU 사용량",
202 .pl = "Użycie CPU",
203 .pt = "Uso da CPU",
204 .ru = "Использование процессора",
205 .tr = "CPU Kullanımı",
206 .uk = "Використання ЦП",
207 .vi = "Mức sử dụng CPU",
208 .zh_CN = "CPU使用率",
209 .zh_TW = "CPU使用率",
210 },
211 .initOptions = (void*) ffInitCPUUsageOptions,
212 .destroyOptions = (void*) ffDestroyCPUUsageOptions,
213 .parseJsonObject = (void*) ffParseCPUUsageJsonObject,
214 .printModule = (void*) ffPrintCPUUsage,
215 .generateJsonResult = (void*) ffGenerateCPUUsageJsonResult,
216 .generateJsonConfig = (void*) ffGenerateCPUUsageJsonConfig,
217 .formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
218 { "CPU usage (percentage num, average)", "avg" },
219 { "CPU usage (percentage num, maximum)", "max" },
220 { "CPU core index of maximum usage", "max-index" },
221 { "CPU usage (percentage num, minimum)", "min" },
222 { "CPU core index of minimum usage", "min-index" },
223 { "CPU usage (percentage bar, average)", "avg-bar" },
224 { "CPU usage (percentage bar, maximum)", "max-bar" },
225 { "CPU usage (percentage bar, minimum)", "min-bar" },
226 })),
227 .defaultOrder = 35,
228};