Skip to content

Commit c8521b5

Browse files
committed
CPUUsage / DiskIO / NetIO: add option waitTime
1 parent 40b8023 commit c8521b5

File tree

12 files changed

+95
-11
lines changed

12 files changed

+95
-11
lines changed

doc/json_schema.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,6 +1181,12 @@
11811181
"description": "Display CPU usage per CPU logical core, instead of an average result",
11821182
"default": false
11831183
},
1184+
"waitTime": {
1185+
"type": "integer",
1186+
"description": "Wait time (in ms). CPU usage = (inUseEnd - inUseStart) / waitTime",
1187+
"default": 200,
1188+
"minimum": 1
1189+
},
11841190
"key": {
11851191
"$ref": "#/$defs/key"
11861192
},
@@ -1472,6 +1478,12 @@
14721478
"type": "boolean",
14731479
"default": false
14741480
},
1481+
"waitTime": {
1482+
"type": "integer",
1483+
"description": "Wait time (in ms). Disk I/O = (totalBytesEnd - totalBytesStart) / waitTime",
1484+
"default": 200,
1485+
"minimum": 1
1486+
},
14751487
"key": {
14761488
"$ref": "#/$defs/key"
14771489
},
@@ -1841,6 +1853,12 @@
18411853
"type": "boolean",
18421854
"default": false
18431855
},
1856+
"waitTime": {
1857+
"type": "integer",
1858+
"description": "Wait time (in ms). Disk I/O = (totalBytesEnd - totalBytesStart) / waitTime",
1859+
"default": 200,
1860+
"minimum": 1
1861+
},
18441862
"key": {
18451863
"$ref": "#/$defs/key"
18461864
},

src/data/help.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,14 @@
916916
"type": "str"
917917
}
918918
},
919+
{
920+
"long": "diskio-wait-time",
921+
"desc": "Set the wait time (in ms) when detecting disk usage",
922+
"arg": {
923+
"type": "num",
924+
"default": 1000
925+
}
926+
},
919927
{
920928
"long": "physicaldisk-name-prefix",
921929
"desc": "Show disks with given name prefix only",
@@ -1070,6 +1078,14 @@
10701078
"default": false
10711079
}
10721080
},
1081+
{
1082+
"long": "cpuusage-wait-time",
1083+
"desc": "Set the wait time (in ms) when detecting CPU usage",
1084+
"arg": {
1085+
"type": "num",
1086+
"default": 200
1087+
}
1088+
},
10731089
{
10741090
"long": "de-slow-version-detection",
10751091
"desc": "Set if DE version should be detected with slow operations",
@@ -1305,6 +1321,14 @@
13051321
"default": false
13061322
}
13071323
},
1324+
{
1325+
"long": "netio-wait-time",
1326+
"desc": "Set the wait time (in ms) when detecting network usage",
1327+
"arg": {
1328+
"type": "num",
1329+
"default": 1000
1330+
}
1331+
},
13081332
{
13091333
"long": "publicip-timeout",
13101334
"desc": "Time in milliseconds to wait for the public ip server to respond",

src/detection/cpuusage/cpuusage.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ void ffPrepareCPUUsage(void)
1313
ffGetCpuUsageInfo(&cpuTimes1);
1414
}
1515

16-
const char* ffGetCpuUsageResult(FFlist* result)
16+
const char* ffGetCpuUsageResult(FFCPUUsageOptions* options, FFlist* result)
1717
{
1818
const char* error = NULL;
1919
if(cpuTimes1.elementSize == 0)
2020
{
2121
ffListInit(&cpuTimes1, sizeof(FFCpuUsageInfo));
2222
error = ffGetCpuUsageInfo(&cpuTimes1);
2323
if(error) return error;
24-
ffTimeSleep(200);
24+
ffTimeSleep(options->waitTime);
2525
}
2626

2727
if(cpuTimes1.length == 0) return "No CPU cores found";
@@ -43,7 +43,7 @@ const char* ffGetCpuUsageResult(FFlist* result)
4343
if (++retryCount <= 3)
4444
{
4545
ffListClear(&cpuTimes2);
46-
ffTimeSleep(200);
46+
ffTimeSleep(options->waitTime);
4747
goto retry;
4848
}
4949
}

src/detection/cpuusage/cpuusage.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ typedef struct FFCpuUsageInfo {
88
} FFCpuUsageInfo;
99
const char* ffGetCpuUsageInfo(FFlist* cpuTimes);
1010

11-
const char* ffGetCpuUsageResult(FFlist* result); // list of double
11+
const char* ffGetCpuUsageResult(FFCPUUsageOptions* options, FFlist* result); // list of double

src/detection/diskio/diskio.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ const char* ffDetectDiskIO(FFlist* result, FFDiskIOOptions* options)
4141
return "No physical disk found";
4242

4343
uint64_t time2 = ffTimeGetNow();
44-
while (time2 - time1 < 1000)
44+
while (time2 - time1 < options->waitTime)
4545
{
46-
ffTimeSleep((uint32_t) (1000 - (time2 - time1)));
46+
ffTimeSleep((uint32_t) (options->waitTime - (time2 - time1)));
4747
time2 = ffTimeGetNow();
4848
}
4949

src/detection/netio/netio.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ const char* ffDetectNetIO(FFlist* result, FFNetIOOptions* options)
4141
return "No network interfaces found";
4242

4343
uint64_t time2 = ffTimeGetNow();
44-
while (time2 - time1 < 1000)
44+
while (time2 - time1 < options->waitTime)
4545
{
46-
ffTimeSleep((uint32_t) (1000 - (time2 - time1)));
46+
ffTimeSleep((uint32_t) (options->waitTime - (time2 - time1)));
4747
time2 = ffTimeGetNow();
4848
}
4949

src/modules/cpuusage/cpuusage.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
void ffPrintCPUUsage(FFCPUUsageOptions* options)
1212
{
1313
FF_LIST_AUTO_DESTROY percentages = ffListCreate(sizeof(double));
14-
const char* error = ffGetCpuUsageResult(&percentages);
14+
const char* error = ffGetCpuUsageResult(options, &percentages);
1515

1616
if(error)
1717
{
@@ -111,6 +111,12 @@ bool ffParseCPUUsageCommandOptions(FFCPUUsageOptions* options, const char* key,
111111
return true;
112112
}
113113

114+
if (ffStrEqualsIgnCase(subKey, "wait-time"))
115+
{
116+
options->waitTime = ffOptionParseUInt32(key, value);
117+
return true;
118+
}
119+
114120
if (ffPercentParseCommandOptions(key, subKey, value, &options->percent))
115121
return true;
116122

@@ -136,6 +142,12 @@ void ffParseCPUUsageJsonObject(FFCPUUsageOptions* options, yyjson_val* module)
136142
continue;
137143
}
138144

145+
if (ffStrEqualsIgnCase(key, "waitTime"))
146+
{
147+
options->waitTime = (uint32_t) yyjson_get_uint(val);
148+
continue;
149+
}
150+
139151
if (ffPercentParseJsonObject(key, val, &options->percent))
140152
continue;
141153

@@ -156,10 +168,10 @@ void ffGenerateCPUUsageJsonConfig(FFCPUUsageOptions* options, yyjson_mut_doc* do
156168
ffPercentGenerateJsonConfig(doc, module, defaultOptions.percent, options->percent);
157169
}
158170

159-
void ffGenerateCPUUsageJsonResult(FF_MAYBE_UNUSED FFCPUUsageOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
171+
void ffGenerateCPUUsageJsonResult(FFCPUUsageOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
160172
{
161173
FF_LIST_AUTO_DESTROY percentages = ffListCreate(sizeof(double));
162-
const char* error = ffGetCpuUsageResult(&percentages);
174+
const char* error = ffGetCpuUsageResult(options, &percentages);
163175

164176
if(error)
165177
{
@@ -203,6 +215,7 @@ void ffInitCPUUsageOptions(FFCPUUsageOptions* options)
203215
ffOptionInitModuleArg(&options->moduleArgs, "󰓅");
204216
options->separate = false;
205217
options->percent = (FFColorRangeConfig) { 50, 80 };
218+
options->waitTime = 200;
206219
}
207220

208221
void ffDestroyCPUUsageOptions(FFCPUUsageOptions* options)

src/modules/cpuusage/option.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ typedef struct FFCPUUsageOptions
1212

1313
bool separate;
1414
FFColorRangeConfig percent;
15+
uint32_t waitTime; // in ms
1516
} FFCPUUsageOptions;

src/modules/diskio/diskio.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ bool ffParseDiskIOCommandOptions(FFDiskIOOptions* options, const char* key, cons
115115
return true;
116116
}
117117

118+
if (ffStrEqualsIgnCase(subKey, "wait-time"))
119+
{
120+
options->waitTime = ffOptionParseUInt32(key, value);
121+
return true;
122+
}
123+
118124
return false;
119125
}
120126

@@ -143,6 +149,12 @@ void ffParseDiskIOJsonObject(FFDiskIOOptions* options, yyjson_val* module)
143149
continue;
144150
}
145151

152+
if (ffStrEqualsIgnCase(key, "waitTime"))
153+
{
154+
options->waitTime = (uint32_t) yyjson_get_uint(val);
155+
continue;
156+
}
157+
146158
ffPrintError(FF_DISKIO_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", key);
147159
}
148160
}
@@ -222,6 +234,7 @@ void ffInitDiskIOOptions(FFDiskIOOptions* options)
222234

223235
ffStrbufInit(&options->namePrefix);
224236
options->detectTotal = false;
237+
options->waitTime = 1000;
225238
}
226239

227240
void ffDestroyDiskIOOptions(FFDiskIOOptions* options)

src/modules/diskio/option.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ typedef struct FFDiskIOOptions
1010
FFModuleArgs moduleArgs;
1111

1212
FFstrbuf namePrefix;
13+
uint32_t waitTime;
1314
bool detectTotal;
1415
} FFDiskIOOptions;

0 commit comments

Comments
 (0)