-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathmonitoring.c
137 lines (122 loc) · 4.22 KB
/
monitoring.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* Module : Data.Array.Accelerate.Debug.Monitoring
* Copyright : [2016..2020] The Accelerate Team
* License : BSD3
*
* Maintainer : Trevor L. McDonell <[email protected]>
* Stability : experimental
* Portability : non-portable (GHC extensions)
*
* Support for runtime system monitoring
*
* This is a hack to work around <https://github.com/haskell/cabal/issues/4937>
*/
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include "align.h"
#include "flags.h"
/* These monitoring counters are globals which will be accessed from the
* Haskell side.
*/
int64_t DOUBLE_CACHE_ALIGNED __total_bytes_allocated_local = 0;
int64_t DOUBLE_CACHE_ALIGNED __total_bytes_allocated_remote = 0;
int64_t DOUBLE_CACHE_ALIGNED __total_bytes_copied_to_remote = 0;
int64_t DOUBLE_CACHE_ALIGNED __total_bytes_copied_from_remote = 0;
int64_t DOUBLE_CACHE_ALIGNED __total_bytes_evicted_from_remote = 0;
int64_t DOUBLE_CACHE_ALIGNED __num_remote_gcs = 0;
int64_t DOUBLE_CACHE_ALIGNED __num_evictions = 0;
/* cbits/flags.c */
extern __flags_t __cmd_line_flags;
#if defined(ACCELERATE_DEBUG)
/* cbits/clock.c */
double clock_gettime_elapsed_seconds(void);
/*
* Format a large number, using comma separators.
*/
static char* format_int64(char *buffer, int64_t x)
{
char *s = buffer;
if (x < 0)
{
*s++ = '-';
x = -x;
}
if (x < 1000)
{
sprintf(s, "%"PRIi64, x);
}
else if (x < 1000000)
{
sprintf(s, "%"PRIi64",%03"PRIi64, x/1000, x%1000);
}
else if (x < 1000000000)
{
sprintf(s, "%"PRIi64",%03"PRIi64",%03"PRIi64
, x/1000000
, (x/1000)%1000
, x%1000);
}
else if (x < 1000000000000)
{
sprintf(s, "%"PRIi64",%03"PRIi64",%03"PRIi64",%03"PRIi64
, x/1000000000
, (x/1000000)%1000
, (x/1000)%1000
, x%1000);
}
else if (x < 1000000000000000)
{
sprintf(s, "%"PRIi64",%03"PRIi64",%03"PRIi64",%03"PRIi64",%03"PRIi64
, x/1000000000000
, (x/1000000000)%1000
, (x/1000000)%1000
, (x/1000)%1000
, x%1000);
}
else if (x < 1000000000000000000)
{
sprintf(s, "%"PRIi64",%03"PRIi64",%03"PRIi64",%03"PRIi64",%03"PRIi64",%03"PRIi64
, x/1000000000000000
, (x/1000000000000)%1000
, (x/1000000000)%1000
, (x/1000000)%1000
, (x/1000)%1000
, x%1000);
}
else
{
sprintf(s, "%"PRIi64",%03"PRIi64",%03"PRIi64",%03"PRIi64",%03"PRIi64",%03"PRIi64",%03"PRIi64
, x/1000000000000000000
, (x/1000000000000000)%1000
, (x/1000000000000)%1000
, (x/1000000000)%1000
, (x/1000000)%1000
, (x/1000)%1000
, x%1000);
}
return buffer;
}
/*
* This function runs after main(), and is used to print final GC and memory
* statistics (if enabled). This is similar to the +RTS -s option.
*/
__attribute__((destructor)) void dump_gc_stats(void)
{
if (__cmd_line_flags.dump_gc_stats) {
/*
* int64 ranges from -9223372036854775807..9223372036854775807, so we need a
* buffer size of at least 27 characters (including the terminating \0) to
* format any number with commas.
*/
char buffer[96];
double timestamp = clock_gettime_elapsed_seconds();
fprintf(stderr, "\n");
fprintf(stderr, "[%8.3f] gc: %s bytes allocated locally\n", timestamp, format_int64(buffer, __total_bytes_allocated_local));
fprintf(stderr, "[%8.3f] gc: %s bytes allocated on the remote device\n", timestamp, format_int64(buffer, __total_bytes_allocated_remote));
fprintf(stderr, "[%8.3f] gc: %s bytes copied to the remote device\n", timestamp, format_int64(buffer, __total_bytes_copied_to_remote));
fprintf(stderr, "[%8.3f] gc: %s bytes copied from the remote device\n", timestamp, format_int64(buffer, __total_bytes_copied_from_remote));
fprintf(stderr, "[%8.3f] gc: %s bytes evicted from the remote (%s evictions, %s GCs)\n", timestamp, format_int64(&buffer[0], __total_bytes_evicted_from_remote), format_int64(&buffer[32], __num_evictions), format_int64(&buffer[64], __num_remote_gcs));
}
}
#endif /* ACCELERATE_DEBUG */