Skip to content

Commit a2327b7

Browse files
committed
GPU: support percentage num & bar in custom format
Fix #1583
1 parent b76ada7 commit a2327b7

File tree

1 file changed

+65
-8
lines changed

1 file changed

+65
-8
lines changed

src/modules/gpu/gpu.c

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ static void printGPUResult(FFGPUOptions* options, uint8_t index, const FFGPUResu
2020
default: type = "Unknown"; break;
2121
}
2222

23+
FFPercentageTypeFlags percentType = options->percent.type == 0 ? instance.config.display.percentType : options->percent.type;
24+
2325
if(options->moduleArgs.outputFormat.length == 0)
2426
{
2527
ffPrintLogoAndKey(FF_GPU_MODULE_NAME, index, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
@@ -53,16 +55,28 @@ static void printGPUResult(FFGPUOptions* options, uint8_t index, const FFGPUResu
5355
{
5456
ffStrbufAppendS(&output, " (");
5557

56-
if(gpu->dedicated.used != FF_GPU_VMEM_SIZE_UNSET)
58+
if (!(percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT))
5759
{
58-
ffParseSize(gpu->dedicated.used, &output);
59-
ffStrbufAppendS(&output, " / ");
60+
if(gpu->dedicated.used != FF_GPU_VMEM_SIZE_UNSET)
61+
{
62+
ffParseSize(gpu->dedicated.used, &output);
63+
ffStrbufAppendS(&output, " / ");
64+
}
65+
ffParseSize(gpu->dedicated.total, &output);
6066
}
61-
ffParseSize(gpu->dedicated.total, &output);
6267
if(gpu->dedicated.used != FF_GPU_VMEM_SIZE_UNSET)
6368
{
64-
ffStrbufAppendS(&output, ", ");
65-
ffPercentAppendNum(&output, (double) gpu->dedicated.used / (double) gpu->dedicated.total * 100.0, options->percent, false, &options->moduleArgs);
69+
double percent = (double) gpu->dedicated.used / (double) gpu->dedicated.total * 100.0;
70+
if (percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
71+
{
72+
ffStrbufAppendS(&output, ", ");
73+
ffPercentAppendNum(&output, percent, options->percent, false, &options->moduleArgs);
74+
}
75+
if (percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
76+
{
77+
ffStrbufAppendS(&output, " ");
78+
ffPercentAppendBar(&output, percent, options->percent, &options->moduleArgs);
79+
}
6680
}
6781
ffStrbufAppendC(&output, ')');
6882
}
@@ -78,16 +92,47 @@ static void printGPUResult(FFGPUOptions* options, uint8_t index, const FFGPUResu
7892
ffTempsAppendNum(gpu->temperature, &tempStr, options->tempConfig, &options->moduleArgs);
7993
FF_STRBUF_AUTO_DESTROY dTotal = ffStrbufCreate();
8094
FF_STRBUF_AUTO_DESTROY dUsed = ffStrbufCreate();
81-
FF_STRBUF_AUTO_DESTROY sTotal = ffStrbufCreate();
82-
FF_STRBUF_AUTO_DESTROY sUsed = ffStrbufCreate();
95+
FF_STRBUF_AUTO_DESTROY dPercentNum = ffStrbufCreate();
96+
FF_STRBUF_AUTO_DESTROY dPercentBar = ffStrbufCreate();
8397
if (gpu->dedicated.total != FF_GPU_VMEM_SIZE_UNSET) ffParseSize(gpu->dedicated.total, &dTotal);
8498
if (gpu->dedicated.used != FF_GPU_VMEM_SIZE_UNSET) ffParseSize(gpu->dedicated.used, &dUsed);
99+
if (gpu->dedicated.total != FF_GPU_VMEM_SIZE_UNSET && gpu->dedicated.used != FF_GPU_VMEM_SIZE_UNSET)
100+
{
101+
double percent = (double) gpu->dedicated.used / (double) gpu->dedicated.total * 100.0;
102+
if (percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
103+
ffPercentAppendNum(&dPercentNum, percent, options->percent, false, &options->moduleArgs);
104+
if (percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
105+
ffPercentAppendBar(&dPercentBar, percent, options->percent, &options->moduleArgs);
106+
}
107+
108+
FF_STRBUF_AUTO_DESTROY sTotal = ffStrbufCreate();
109+
FF_STRBUF_AUTO_DESTROY sUsed = ffStrbufCreate();
110+
FF_STRBUF_AUTO_DESTROY sPercentNum = ffStrbufCreate();
111+
FF_STRBUF_AUTO_DESTROY sPercentBar = ffStrbufCreate();
85112
if (gpu->shared.total != FF_GPU_VMEM_SIZE_UNSET) ffParseSize(gpu->shared.total, &sTotal);
86113
if (gpu->shared.used != FF_GPU_VMEM_SIZE_UNSET) ffParseSize(gpu->shared.used, &sUsed);
114+
if (gpu->shared.total != FF_GPU_VMEM_SIZE_UNSET && gpu->shared.used != FF_GPU_VMEM_SIZE_UNSET)
115+
{
116+
double percent = (double) gpu->shared.used / (double) gpu->shared.total * 100.0;
117+
if (percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
118+
ffPercentAppendNum(&sPercentNum, percent, options->percent, false, &options->moduleArgs);
119+
if (percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
120+
ffPercentAppendBar(&sPercentBar, percent, options->percent, &options->moduleArgs);
121+
}
87122

88123
FF_STRBUF_AUTO_DESTROY frequency = ffStrbufCreate();
89124
ffParseFrequency(gpu->frequency, &frequency);
90125

126+
FF_STRBUF_AUTO_DESTROY coreUsageNum = ffStrbufCreate();
127+
FF_STRBUF_AUTO_DESTROY coreUsageBar = ffStrbufCreate();
128+
if (gpu->coreUsage == gpu->coreUsage) //FF_GPU_CORE_USAGE_UNSET
129+
{
130+
if (percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
131+
ffPercentAppendNum(&coreUsageNum, gpu->coreUsage, options->percent, false, &options->moduleArgs);
132+
if (percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
133+
ffPercentAppendBar(&coreUsageBar, gpu->coreUsage, options->percent, &options->moduleArgs);
134+
}
135+
91136
FF_PRINT_FORMAT_CHECKED(FF_GPU_MODULE_NAME, index, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
92137
FF_FORMAT_ARG(gpu->vendor, "vendor"),
93138
FF_FORMAT_ARG(gpu->name, "name"),
@@ -102,6 +147,12 @@ static void printGPUResult(FFGPUOptions* options, uint8_t index, const FFGPUResu
102147
FF_FORMAT_ARG(gpu->platformApi, "platform-api"),
103148
FF_FORMAT_ARG(frequency, "frequency"),
104149
FF_FORMAT_ARG(index, "index"),
150+
FF_FORMAT_ARG(dPercentNum, "dedicated-percentage-num"),
151+
FF_FORMAT_ARG(dPercentBar, "dedicated-percentage-bar"),
152+
FF_FORMAT_ARG(sPercentNum, "shared-percentage-num"),
153+
FF_FORMAT_ARG(sPercentBar, "shared-percentage-bar"),
154+
FF_FORMAT_ARG(coreUsageNum, "core-usage-num"),
155+
FF_FORMAT_ARG(coreUsageBar, "core-usage-bar"),
105156
}));
106157
}
107158
}
@@ -414,6 +465,12 @@ static FFModuleBaseInfo ffModuleInfo = {
414465
{"The platform API used when detecting the GPU", "platform-api"},
415466
{"Current frequency in GHz", "frequency"},
416467
{"GPU vendor specific index", "index"},
468+
{"Dedicated memory usage percentage num", "dedicated-percentage-num"},
469+
{"Dedicated memory usage percentage bar", "dedicated-percentage-bar"},
470+
{"Shared memory usage percentage num", "shared-percentage-num"},
471+
{"Shared memory usage percentage bar", "shared-percentage-bar"},
472+
{"Core usage percentage num (supports Nvidia & Apple GPU only)", "core-usage-num"},
473+
{"Core usage percentage bar (supports Nvidia & Apple GPU only)", "core-usage-bar"},
417474
})),
418475
};
419476

0 commit comments

Comments
 (0)