Skip to content

Instantly share code, notes, and snippets.

@bplotnick
Created October 27, 2016 07:14
Show Gist options
  • Save bplotnick/87f18e199f3b3f7fb1bd4941f5d7213c to your computer and use it in GitHub Desktop.
Save bplotnick/87f18e199f3b3f7fb1bd4941f5d7213c to your computer and use it in GitHub Desktop.
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
void get_memusage_extra(uint64_t * uss, uint64_t * pss) {
FILE *file = fopen("/proc/self/smaps", "r");
char line [BUFSIZ];
while (fgets(line, sizeof line, file))
{
char substr[32];
int n;
if (sscanf(line, "%31[^:]: %d", substr, &n) == 2)
{
if (strcmp(substr, "Private_Clean") == 0) { *uss += n * 1024; }
else if (strcmp(substr, "Private_Dirty") == 0) { *uss += n * 1024; }
else if (strcmp(substr, "Pss") == 0) { *pss += n * 1024; }
}
}
fclose(file);
}
int main() {
struct timeval before, after;
int iterations = 100000;
int i;
uint64_t uss;
uint64_t pss;
gettimeofday(&before, NULL);
for (i = 0; i < iterations; ++i) {
get_memusage_extra(&uss, &pss);
}
gettimeofday(&after, NULL);
double duration = (after.tv_sec * 1000000 + after.tv_usec)
- (before.tv_sec * 1000000 + before.tv_usec);
printf("%f microseconds per iteration\n", (duration)/(iterations));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment