Skip to content

Commit 94f2afc

Browse files
committed
[core] Avoid code duplication in Linux system info functions
Change on the side to make sure the number of lines of code doesn't increse too much.
1 parent 47caba5 commit 94f2afc

File tree

1 file changed

+30
-56
lines changed

1 file changed

+30
-56
lines changed

core/unix/src/TUnixSystem.cxx

Lines changed: 30 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5111,6 +5111,19 @@ static void GetDarwinProcInfo(ProcInfo_t *procinfo)
51115111
#endif
51125112

51135113
#if defined(R__LINUX)
5114+
5115+
namespace {
5116+
bool parseLine(TString &s, const char *prefix, Int_t &field)
5117+
{
5118+
if (s.BeginsWith(prefix)) {
5119+
TPRegexp{"^.+: *([^ ]+).*"}.Substitute(s, "$1");
5120+
field = (s.Atoi() / 1024);
5121+
return true;
5122+
}
5123+
return false;
5124+
};
5125+
} // namespace
5126+
51145127
////////////////////////////////////////////////////////////////////////////////
51155128
/// Get system info for Linux. Only fBusSpeed is not set.
51165129

@@ -5124,17 +5137,9 @@ static void GetLinuxSysInfo(SysInfo_t *sysinfo)
51245137
TPRegexp("^.+: *(.*$)").Substitute(s, "$1");
51255138
sysinfo->fModel = s;
51265139
}
5127-
if (s.BeginsWith("cpu MHz")) {
5128-
TPRegexp("^.+: *([^ ]+).*").Substitute(s, "$1");
5129-
sysinfo->fCpuSpeed = s.Atoi();
5130-
}
5131-
if (s.BeginsWith("cache size")) {
5132-
TPRegexp("^.+: *([^ ]+).*").Substitute(s, "$1");
5133-
sysinfo->fL2Cache = s.Atoi();
5134-
}
5135-
if (s.BeginsWith("processor")) {
5136-
TPRegexp("^.+: *([^ ]+).*").Substitute(s, "$1");
5137-
sysinfo->fCpus = s.Atoi();
5140+
parseLine(s, "cpu MHz", sysinfo->fCpuSpeed);
5141+
parseLine(s, "cache size", sysinfo->fL2Cache);
5142+
if (parseLine(s, "processor", sysinfo->fCpus)) {
51385143
sysinfo->fCpus++;
51395144
}
51405145
}
@@ -5144,9 +5149,7 @@ static void GetLinuxSysInfo(SysInfo_t *sysinfo)
51445149
f = fopen("/proc/meminfo", "r");
51455150
if (f) {
51465151
while (s.Gets(f)) {
5147-
if (s.BeginsWith("MemTotal")) {
5148-
TPRegexp("^.+: *([^ ]+).*").Substitute(s, "$1");
5149-
sysinfo->fPhysRam = (s.Atoi() / 1024);
5152+
if (parseLine(s, "MemTotal", sysinfo->fPhysRam)) {
51505153
break;
51515154
}
51525155
}
@@ -5225,49 +5228,20 @@ static void GetLinuxMemInfo(MemInfo_t *meminfo)
52255228
{
52265229
TString s;
52275230
FILE *f = fopen("/proc/meminfo", "r");
5228-
if (!f) return;
5231+
if (!f)
5232+
return;
5233+
52295234
while (s.Gets(f)) {
5230-
if (s.BeginsWith("MemTotal")) {
5231-
TPRegexp("^.+: *([^ ]+).*").Substitute(s, "$1");
5232-
meminfo->fMemTotal = (s.Atoi() / 1024);
5233-
}
5234-
if (s.BeginsWith("MemFree")) {
5235-
TPRegexp("^.+: *([^ ]+).*").Substitute(s, "$1");
5236-
meminfo->fMemFree = (s.Atoi() / 1024);
5237-
}
5238-
if (s.BeginsWith("MemAvailable")) {
5239-
TPRegexp("^.+: *([^ ]+).*").Substitute(s, "$1");
5240-
meminfo->fMemAvailable = (s.Atoi() / 1024);
5241-
}
5242-
if (s.BeginsWith("Cached")) {
5243-
TPRegexp("^.+: *([^ ]+).*").Substitute(s, "$1");
5244-
meminfo->fMemCached = (s.Atoi() / 1024);
5245-
}
5246-
if (s.BeginsWith("Buffers")) {
5247-
TPRegexp("^.+: *([^ ]+).*").Substitute(s, "$1");
5248-
meminfo->fMemBuffer = (s.Atoi() / 1024);
5249-
}
5250-
if (s.BeginsWith("Shmem")) {
5251-
TPRegexp("^.+: *([^ ]+).*").Substitute(s, "$1");
5252-
meminfo->fMemShared = (s.Atoi() / 1024);
5253-
}
5254-
if (s.BeginsWith("SwapTotal")) {
5255-
TPRegexp("^.+: *([^ ]+).*").Substitute(s, "$1");
5256-
meminfo->fSwapTotal = (s.Atoi() / 1024);
5257-
}
5258-
if (s.BeginsWith("SwapFree")) {
5259-
TPRegexp("^.+: *([^ ]+).*").Substitute(s, "$1");
5260-
meminfo->fSwapFree = (s.Atoi() / 1024);
5261-
}
5262-
if (s.BeginsWith("SwapCached")) {
5263-
TPRegexp("^.+: *([^ ]+).*").Substitute(s, "$1");
5264-
meminfo->fSwapCached = (s.Atoi() / 1024);
5265-
}
5266-
if (s.BeginsWith("SReclaimable")) {
5267-
TPRegexp("^.+: *([^ ]+).*").Substitute(s, "$1");
5268-
meminfo->fSReclaimable = (s.Atoi() / 1024);
5269-
}
5270-
5235+
parseLine(s, "MemTotal", meminfo->fMemTotal);
5236+
parseLine(s, "MemFree", meminfo->fMemFree);
5237+
parseLine(s, "MemAvailable", meminfo->fMemAvailable);
5238+
parseLine(s, "Cached", meminfo->fMemCached);
5239+
parseLine(s, "Buffers", meminfo->fMemBuffer);
5240+
parseLine(s, "Shmem", meminfo->fMemShared);
5241+
parseLine(s, "SwapTotal", meminfo->fSwapTotal);
5242+
parseLine(s, "SwapFree", meminfo->fSwapFree);
5243+
parseLine(s, "SwapCached", meminfo->fSwapCached);
5244+
parseLine(s, "SReclaimable", meminfo->fSReclaimable);
52715245
}
52725246
fclose(f);
52735247

0 commit comments

Comments
 (0)