-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmeter.c
91 lines (69 loc) · 1.96 KB
/
meter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* Copyright (c) 2010, the Short-term Memory Project Authors.
* All rights reserved. Please see the AUTHORS file for details.
* Use of this source code is governed by a BSD license that
* can be found in the LICENSE file.
*/
#include "meter.h"
#ifdef SCM_RECORD_MEMORY_USAGE
static long alloc_mem = 0;
static long num_alloc = 0;
/**
* Keeps track of the allocated memory
*/
void inc_allocated_mem(long inc) {
//alloc_mem += inc;
__sync_add_and_fetch(&alloc_mem, inc);
__sync_add_and_fetch(&num_alloc, 1);
}
static long freed_mem = 0;
static long num_freed = 0;
/**
* Keeps track of the freed memory
*/
void inc_freed_mem(long inc) {
//freed_mem += inc;
__sync_add_and_fetch(&freed_mem, inc);
__sync_add_and_fetch(&num_freed, 1);
}
static long pooled_mem = 0;
/**
* Keeps track of the pooled memory
*/
void inc_pooled_mem(long inc) {
//pooled_mem += inc;
__sync_add_and_fetch(&pooled_mem, inc);
}
void dec_pooled_mem(long inc) {
//pooled_mem -= inc;
__sync_sub_and_fetch(&pooled_mem, inc);
}
static long mem_overhead = 0 ;
/**
* Keeps track of memory overhead
*/
void inc_overhead(long inc) {
//mem_overhead += inc;
__sync_add_and_fetch(&mem_overhead, inc);
}
void dec_overhead(long inc) {
//mem_overhead -= inc;
__sync_sub_and_fetch(&mem_overhead, inc);
}
static long start_time = 0;
/**
* Prints the memory consumption for total, pooled, and used memory
*/
void print_memory_consumption() {
struct timeval t;
gettimeofday(&t, NULL);
long usec = t.tv_sec * 1000000 + t.tv_usec;
if (start_time == 0) {
start_time = usec;
}
// struct mallinfo info = mallinfo();
printf("memory usage:\t%lu\t%ld\t%ld\t%ld\n", usec - start_time, alloc_mem - freed_mem, pooled_mem, alloc_mem - pooled_mem);
printf("memory overhead:\t%lu\t%lu\n", usec - start_time, mem_overhead);
// printf("mallinfo:\t%lu\t%d\n", usec - start_time, info.uordblks);
}
#endif /* SCM_RECORD_MEMORY_USAGE */