aboutsummaryrefslogtreecommitdiff
path: root/procps
diff options
context:
space:
mode:
Diffstat (limited to 'procps')
-rw-r--r--procps/top.c56
1 files changed, 27 insertions, 29 deletions
diff --git a/procps/top.c b/procps/top.c
index 494509c7c..c02a959e0 100644
--- a/procps/top.c
+++ b/procps/top.c
@@ -212,6 +212,31 @@ static void do_stats(void)
212} 212}
213#endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */ 213#endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
214 214
215#if ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS && ENABLE_FEATURE_TOP_DECIMALS
216/* formats 7 char string (8 with terminating NUL) */
217static char *fmt_100percent_8(char pbuf[8], unsigned value, unsigned total)
218{
219 unsigned t;
220 if (value >= total) { /* 100% ? */
221 strcpy(pbuf, " 100% ");
222 return pbuf;
223 }
224 /* else generate " [N/space]N.N% " string */
225 value = 1000 * value / total;
226 t = value / 100;
227 value = value % 100;
228 pbuf[0] = ' ';
229 pbuf[1] = t ? t + '0' : ' ';
230 pbuf[2] = '0' + (value / 10);
231 pbuf[3] = '.';
232 pbuf[4] = '0' + (value % 10);
233 pbuf[5] = '%';
234 pbuf[6] = ' ';
235 pbuf[7] = '\0';
236 return pbuf;
237}
238#endif
239
215/* display generic info (meminfo / loadavg) */ 240/* display generic info (meminfo / loadavg) */
216static unsigned long display_generic(int scr_width) 241static unsigned long display_generic(int scr_width)
217{ 242{
@@ -219,37 +244,10 @@ static unsigned long display_generic(int scr_width)
219 char buf[80]; 244 char buf[80];
220 char scrbuf[80]; 245 char scrbuf[80];
221 unsigned long total, used, mfree, shared, buffers, cached; 246 unsigned long total, used, mfree, shared, buffers, cached;
222#if ENABLE_FEATURE_TOP_DECIMALS || ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS 247#if ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS
223 unsigned total_diff; 248 unsigned total_diff;
224#endif 249#endif
225 250
226#if ENABLE_FEATURE_TOP_DECIMALS
227 /* formats 7 char string (8 with terminating NUL) */
228 /* using GCCism (nested function) - we need to access total_diff */
229 /* This produces more than 100 bytes smaller code */
230 char *fmt_100percent_8(char pbuf[8], unsigned value)
231 {
232 unsigned t;
233 if (value >= total_diff) { /* 100% ? */
234 strcpy(pbuf, " 100% ");
235 return pbuf;
236 }
237 /* else generate " [N/space]N.N% " string */
238 value = 1000 * value / total_diff;
239 t = value / 100;
240 value = value % 100;
241 pbuf[0] = ' ';
242 pbuf[1] = t ? t + '0' : ' ';
243 pbuf[2] = '0' + (value / 10);
244 pbuf[3] = '.';
245 pbuf[4] = '0' + (value % 10);
246 pbuf[5] = '%';
247 pbuf[6] = ' ';
248 pbuf[7] = '\0';
249 return pbuf;
250 }
251#endif
252
253 /* read memory info */ 251 /* read memory info */
254 fp = xfopen("meminfo", "r"); 252 fp = xfopen("meminfo", "r");
255 253
@@ -316,7 +314,7 @@ static unsigned long display_generic(int scr_width)
316#if ENABLE_FEATURE_TOP_DECIMALS 314#if ENABLE_FEATURE_TOP_DECIMALS
317/* Generated code is approx +0.3k */ 315/* Generated code is approx +0.3k */
318#define CALC_STAT(xxx) char xxx[8] 316#define CALC_STAT(xxx) char xxx[8]
319#define SHOW_STAT(xxx) fmt_100percent_8(xxx, (unsigned)(jif.xxx - prev_jif.xxx)) 317#define SHOW_STAT(xxx) fmt_100percent_8(xxx, (unsigned)(jif.xxx - prev_jif.xxx), total_diff)
320#define FMT "%s" 318#define FMT "%s"
321#else 319#else
322#define CALC_STAT(xxx) unsigned xxx = 100 * (unsigned)(jif.xxx - prev_jif.xxx) / total_diff 320#define CALC_STAT(xxx) unsigned xxx = 100 * (unsigned)(jif.xxx - prev_jif.xxx) / total_diff