| 1 |
|
| #include "EXTERN.h" |
| 2 |
|
| #include "perl.h" |
| 3 |
|
| #include "XSUB.h" |
| 4 |
|
| |
| 5 |
|
| #include <stdio.h> |
| 6 |
|
| #include <string.h> |
| 7 |
|
| #include <sys/mman.h> |
| 8 |
|
| |
| 9 |
|
| struct smaps_sizes { |
| 10 |
|
| int KernelPageSize; |
| 11 |
|
| int MMUPageSize; |
| 12 |
|
| int Private_Clean; |
| 13 |
|
| int Private_Dirty; |
| 14 |
|
| int Pss; |
| 15 |
|
| int Referenced; |
| 16 |
|
| int Rss; |
| 17 |
|
| int Shared_Clean; |
| 18 |
|
| int Shared_Dirty; |
| 19 |
|
| int Size; |
| 20 |
|
| int Swap; |
| 21 |
|
| }; |
| 22 |
|
| |
| 23 |
|
| MODULE = Linux::Smaps::Tiny PACKAGE = Linux::Smaps::Tiny |
| 24 |
|
| PROTOTYPES: DISABLE |
| 25 |
|
| |
| 26 |
|
| SV* |
| 27 |
|
| get_smaps_summary(char* process = "self") |
| 28 |
|
| PPCODE: |
| 29 |
|
| struct smaps_sizes sizes; |
| 30 |
0 |
| memset(&sizes, 0, sizeof sizes); |
| 31 |
|
| |
| 32 |
|
| char filename[100]; |
| 33 |
0 |
| memset(&filename, 0, sizeof filename); |
| 34 |
0 |
| strcat(filename, "/proc/"); |
| 35 |
0 |
| strcat(filename, process); |
| 36 |
0 |
| strcat(filename, "/smaps"); |
| 37 |
|
| |
| 38 |
0 |
| FILE *file = fopen(filename, "r"); |
| 39 |
0 |
▶ | if (!file) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!file) { |
0 |
0 |
0 |
|
| 40 |
0 |
| croak("Failed to read '%s': [%d] %s", filename, errno, strerror(errno)); |
| 41 |
|
| } |
| 42 |
|
| |
| 43 |
|
| char line[100]; |
| 44 |
0 |
▶ | while (fgets(line, sizeof line, file)) |
Branch: 1 branch
| Branch | True | False | Total |
| while (fgets(line, sizeof line, file)) |
0 |
0 |
0 |
|
| 45 |
|
| { |
| 46 |
|
| char substr[32]; |
| 47 |
|
| int n; |
| 48 |
0 |
▶ | if (sscanf(line, "%32[^:]: %d", substr, &n) == 2) |
Branch: 1 branch
| Branch | True | False | Total |
| if (sscanf(line, "%32[^:]: %d", substr, &n) == 2) |
0 |
0 |
0 |
|
| 49 |
|
| { |
| 50 |
|
| /* I'm counting on a modern compiler like GCC or clang to |
| 51 |
|
| * optimize this to a jump table. They can actually do |
| 52 |
|
| * that these days with their fancy technology. |
| 53 |
|
| */ |
| 54 |
0 |
▶ | if (strcmp(substr, "KernelPageSize") == 0) { sizes.KernelPageSize += n; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (strcmp(substr, "KernelPageSize") == 0) { sizes.KernelPageSize += n; } |
0 |
0 |
0 |
|
| 55 |
0 |
▶ | else if (strcmp(substr, "MMUPageSize") == 0) { sizes.MMUPageSize += n; } |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(substr, "MMUPageSize") == 0) { sizes.MMUPageSize += n; } |
0 |
0 |
0 |
|
| 56 |
0 |
▶ | else if (strcmp(substr, "Private_Clean") == 0) { sizes.Private_Clean += n; } |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(substr, "Private_Clean") == 0) { sizes.Private_Clean += n; } |
0 |
0 |
0 |
|
| 57 |
0 |
▶ | else if (strcmp(substr, "Private_Dirty") == 0) { sizes.Private_Dirty += n; } |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(substr, "Private_Dirty") == 0) { sizes.Private_Dirty += n; } |
0 |
0 |
0 |
|
| 58 |
0 |
▶ | else if (strcmp(substr, "Pss") == 0) { sizes.Pss += n; } |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(substr, "Pss") == 0) { sizes.Pss += n; } |
0 |
0 |
0 |
|
| 59 |
0 |
▶ | else if (strcmp(substr, "Referenced") == 0) { sizes.Referenced += n; } |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(substr, "Referenced") == 0) { sizes.Referenced += n; } |
0 |
0 |
0 |
|
| 60 |
0 |
▶ | else if (strcmp(substr, "Rss") == 0) { sizes.Rss += n; } |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(substr, "Rss") == 0) { sizes.Rss += n; } |
0 |
0 |
0 |
|
| 61 |
0 |
▶ | else if (strcmp(substr, "Shared_Clean") == 0) { sizes.Shared_Clean += n; } |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(substr, "Shared_Clean") == 0) { sizes.Shared_Clean += n; } |
0 |
0 |
0 |
|
| 62 |
0 |
▶ | else if (strcmp(substr, "Shared_Dirty") == 0) { sizes.Shared_Dirty += n; } |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(substr, "Shared_Dirty") == 0) { sizes.Shared_Dirty += n; } |
0 |
0 |
0 |
|
| 63 |
0 |
▶ | else if (strcmp(substr, "Size") == 0) { sizes.Size += n; } |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(substr, "Size") == 0) { sizes.Size += n; } |
0 |
0 |
0 |
|
| 64 |
0 |
▶ | else if (strcmp(substr, "Swap") == 0) { sizes.Swap += n; } |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strcmp(substr, "Swap") == 0) { sizes.Swap += n; } |
0 |
0 |
0 |
|
| 65 |
|
| } |
| 66 |
|
| } |
| 67 |
0 |
| fclose(file); |
| 68 |
|
| |
| 69 |
0 |
| HV* hash = newHV(); |
| 70 |
0 |
| (void)hv_store(hash, "KernelPageSize", strlen("KernelPageSize"), newSViv(sizes.KernelPageSize), 0); |
| 71 |
0 |
| (void)hv_store(hash, "MMUPageSize", strlen("MMUPageSize"), newSViv(sizes.MMUPageSize), 0); |
| 72 |
0 |
| (void)hv_store(hash, "Private_Clean", strlen("Private_Clean"), newSViv(sizes.Private_Clean), 0); |
| 73 |
0 |
| (void)hv_store(hash, "Private_Dirty", strlen("Private_Dirty"), newSViv(sizes.Private_Dirty), 0); |
| 74 |
0 |
| (void)hv_store(hash, "Pss", strlen("Pss"), newSViv(sizes.Pss), 0); |
| 75 |
0 |
| (void)hv_store(hash, "Referenced", strlen("Referenced"), newSViv(sizes.Referenced), 0); |
| 76 |
0 |
| (void)hv_store(hash, "Rss", strlen("Rss"), newSViv(sizes.Rss), 0); |
| 77 |
0 |
| (void)hv_store(hash, "Shared_Clean", strlen("Shared_Clean"), newSViv(sizes.Shared_Clean), 0); |
| 78 |
0 |
| (void)hv_store(hash, "Shared_Dirty", strlen("Shared_Dirty"), newSViv(sizes.Shared_Dirty), 0); |
| 79 |
0 |
| (void)hv_store(hash, "Size", strlen("Size"), newSViv(sizes.Size), 0); |
| 80 |
0 |
| (void)hv_store(hash, "Swap", strlen("Swap"), newSViv(sizes.Swap), 0); |
| 81 |
|
| |
| 82 |
|
| /* CPAN RT #78029 */ |
| 83 |
0 |
| sv_2mortal((SV*) hash); |
| 84 |
|
| |
| 85 |
0 |
▶ | XPUSHs(newRV_noinc((SV*) hash)); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(newRV_noinc((SV*) hash)); |
0 |
0 |
0 |
|