From e9c101edcbf3afeb3d0f49dc31d116834ff9ff42 Mon Sep 17 00:00:00 2001 From: Jim Warner Date: Sun, 21 Aug 2016 00:00:00 -0500 Subject: [PATCH] library: prefer /proc/stat before /proc/status, Long ago, in a galaxy far away (when top was in charge of library FILL flgs) /proc/status was to be preferred over /proc/stat if a field could be satisfied by both. This was done to avoid costly 64-bit math emulation in a 32-bit application due to 'unsigned long long' data. Well it's time to acknowledge the prevalence of 64-bit platforms. And in such an environment the cost picture has shifted significantly. It now costs 14 times (wow) as much to access /proc/status compared to /proc/stat. In other words, even with '%llu' variables, a sscanf() call in stat2proc() beats the pants off that home brew gperf based hashing employed by the status2proc() guy. In fact, status2proc incurs higher costs than found in the most expensive aspect of top's forest view option. Here's a gprof extract to illustrate the costs. It was produced with an rcfile requiring fields from both the /proc/stat & /proc/status pseudo files (among others). There were 5000 iterations in each of 4 separate gprof runs subsequently merged into 1 gmon.sum for analysis. % self self time seconds calls us/call name ----- ------- ------- ------- ----------- 28.65 4.10 4689423 0.87 status2proc 26.14 3.74 40000 93.50 forest_adds ... 01.96 0.28 4689427 0.06 stat2proc [ since forest_adds is recursive, the calls value is ] [ the non-recursive #, its 'call graph' shows totals ] Anyway, now that such cost is known this patch becomes what is euphemistically known as the usual no-brainer. [ jeeze, was it really this long between profilings? ] Signed-off-by: Jim Warner --- proc/pids.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/proc/pids.c b/proc/pids.c index 2976fd72..c42dbc46 100644 --- a/proc/pids.c +++ b/proc/pids.c @@ -339,7 +339,7 @@ srtDECL(noop) { // ___ Controlling Table |||||||||||||||||||||||||||||||||||||||||||||||||||||| // from either 'stat' or 'status' (preferred) -#define f_either PROC_SPARE_1 +#define f_either PROC_SPARE_1 // either status or stat (favor stat) #define f_grp PROC_FILLGRP #define f_lxc PROC_FILL_LXC #define f_ns PROC_FILLNS @@ -913,8 +913,8 @@ static inline void pids_libflags_set ( info->history_yes |= Item_table[e].needhist; } if (info->oldflags & f_either) { - if (!(info->oldflags & f_stat)) - info->oldflags |= f_status; + if (!(info->oldflags & (f_stat | f_status))) + info->oldflags |= f_stat; } return; } // end: pids_libflags_set