File tree Expand file tree Collapse file tree 1 file changed +6
-27
lines changed
Expand file tree Collapse file tree 1 file changed +6
-27
lines changed Original file line number Diff line number Diff line change 11#include "swap.h"
22
3- #include <stdlib.h>
4- #include <string.h>
3+ #include <sys/sysinfo.h>
54
65void ffDetectSwap (FFMemoryStorage * swap )
76{
8- FILE * meminfo = fopen ("/proc/meminfo" , "r" );
9- if (meminfo == NULL )
10- {
11- ffStrbufAppendS (& swap -> error , "Failed to open /proc/meminfo" );
12- return ;
13- }
7+ struct sysinfo info ;
8+ if (sysinfo (& info ) != 0 )
9+ ffStrbufAppendS (& swap -> error , "sysinfo() failed" );
1410
15- char * line = NULL ;
16- size_t len = 0 ;
17-
18- uint32_t swapTotal = 0 ,
19- swapFree = 0 ;
20-
21- while (getline (& line , & len , meminfo ) != EOF )
22- {
23- if (!sscanf (line , "SwapTotal: %u" , & swapTotal ))
24- sscanf (line , "SwapFree: %u" , & swapFree );
25- }
26-
27- if (line != NULL )
28- free (line );
29-
30- fclose (meminfo );
31-
32- swap -> bytesTotal = swapTotal * (uint64_t ) 1024 ;
33- swap -> bytesUsed = (swapTotal - swapFree ) * (uint64_t ) 1024 ;
11+ swap -> bytesTotal = info .totalswap * (uint64_t ) info .mem_unit ;
12+ swap -> bytesUsed = (info .totalswap - info .freeswap ) * (uint64_t ) info .mem_unit ;
3413}
You can’t perform that action at this time.
0 commit comments