Implemented the Preprocessor interface.
[aesalon.git] / modules / informer / src / collector / informer.c
blob4f799b8c1ca27e70e1171fee6101cc19e4f9c8be
1 /**
2 Aesalon, a tool to visualize a program's behaviour at run-time.
3 Copyright (C) 2010, Aesalon Development Team.
5 Aesalon is distributed under the terms of the GNU GPLv3. For more
6 licensing information, see the file LICENSE included with the distribution.
8 @file modules/informer/src/collector/informer.c
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <string.h>
16 #include <fcntl.h>
17 #include <sys/mman.h>
18 #include <sys/stat.h>
19 #include <sys/types.h>
20 #include <dlfcn.h>
21 #include <time.h>
22 #include <pthread.h>
23 #include <errno.h>
25 #include "informer/Informer.h"
26 #include "common/Config.h"
27 #include "common/ZoneHeader.h"
29 typedef struct Zone_t Zone_t;
30 struct Zone_t {
31 int offset;
32 Zone_t *next;
35 typedef struct InformerData_t InformerData_t;
36 struct InformerData_t {
37 int initialized;
38 uint64_t processID;
40 pthread_t monitorThreadList[AesalonInformerMonitorThreadListSize];
41 int monitorThreadListSize;
43 pthread_t *threadList;
44 int threadListSize;
45 int threadCount;
47 int shmFd;
49 SharedMemoryHeader_t *shmHeader;
51 const char *configData;
53 uint8_t *zoneUseData;
56 static InformerData_t AI_InformerData;
58 static __thread uint8_t *AI_Zone = NULL;
59 static __thread SHMPacketHeader_t *AI_ZonePacket = NULL;
61 /** Interally-used function; opens shared memory for later use.
62 @param name The name of the SHM to use.
64 static void AI_OpenSHM(const char *name);
66 static void AI_SetupHeader();
67 static void AI_SetupConfig();
68 static void AI_SetupZoneUse();
70 static void AI_SetupZone();
71 static void *AI_ReserveSpace(uint32_t amount);
73 static int AI_ZoneAvailable(uint32_t id);
74 static void AI_MarkZone(uint32_t id);
75 static void AI_ClearZone(uint32_t id);
77 /** Internally-used function to calculate the amount of space remaining in the zone for
78 the current thread.
79 @param zoneID The zone to calculate the remaining space.
81 static uint32_t AI_RemainingSpace();
83 /* ------------------------------------------------------------------ */
85 void __attribute__((constructor)) AI_Construct() {
86 /* By default, .initialized will be set to 0 (AI_InformerData is a global). */
87 if(AI_InformerData.initialized) return;
88 AI_InformerData.initialized = 1;
90 printf("[AI] **** Constructing Informer . . .\n");
92 pthread_t self = pthread_self();
94 AI_StopCollection(self);
96 pid_t pid = getpid();
98 char filename[1024];
100 int fd = open("/proc/self/cmdline", O_RDONLY);
102 read(fd, filename, sizeof(filename));
104 close(fd);
106 /* String hashing algorithm: djb2. */
108 uint64_t pathHash = 0;
109 int c = 0;
110 char *p = filename;
111 while((c = (*p++))) {
112 pathHash = c + (pathHash << 6) + (pathHash << 16) - pathHash;
115 /* Clear the first 16 bits for the PID to be inserted properly. */
116 pathHash &= ~0xffff;
118 AI_InformerData.processID = pathHash ^ pid;
120 const char *shmName = getenv("AesalonSHMName");
121 if(shmName == NULL) {
122 fprintf(stderr, "[aesalon] AesalonSHMName not set, aborting.\n");
123 exit(1);
125 AI_OpenSHM(shmName);
127 AI_SetupHeader();
128 AI_SetupConfig();
129 AI_SetupZoneUse();
131 AI_InformerData.threadList = malloc(sizeof(pthread_t) * 16);
132 AI_InformerData.threadListSize = 16;
133 AI_InformerData.threadCount = 1;
134 AI_InformerData.threadList[0] = self;
136 AI_ContinueCollection(self);
139 void __attribute__((destructor)) AI_Destruct() {
140 printf("[AI] Destructing Informer . . .\n");
143 static void AI_OpenSHM(const char *name) {
144 AI_InformerData.shmFd = shm_open(name, O_RDWR, S_IRUSR | S_IWUSR);
147 static void AI_SetupHeader() {
148 AI_InformerData.shmHeader = mmap(NULL, AesalonPageSize,
149 PROT_READ | PROT_WRITE, MAP_SHARED, AI_InformerData.shmFd, 0);
152 static void AI_SetupConfig() {
153 AI_InformerData.configData = mmap(NULL, AI_InformerData.shmHeader->configDataSize*AesalonPageSize,
154 PROT_READ | PROT_WRITE, MAP_SHARED, AI_InformerData.shmFd, AesalonPageSize);
157 static void AI_SetupZoneUse() {
158 AI_InformerData.zoneUseData = mmap(NULL, AI_InformerData.shmHeader->zoneUsagePages*AesalonPageSize,
159 PROT_READ | PROT_WRITE, MAP_SHARED, AI_InformerData.shmFd,
160 (AI_InformerData.shmHeader->configDataSize + 1)*AesalonPageSize);
162 /* +1 for the header. */
163 AI_InformerData.shmHeader->zonePageOffset =
164 AI_InformerData.shmHeader->zoneUsagePages + AI_InformerData.shmHeader->configDataSize + 1;
167 static void AI_SetupZone() {
168 /* Check if more memory is required. */
169 while(AI_InformerData.shmHeader->zoneCount >= AI_InformerData.shmHeader->zonesAllocated) {
170 /* Allocate more memory. */
171 sem_wait(&AI_InformerData.shmHeader->resizeSemaphore);
172 if(AI_InformerData.shmHeader->zoneCount >= AI_InformerData.shmHeader->zonesAllocated) {
173 AI_InformerData.shmHeader->shmSize += AI_InformerData.shmHeader->zoneSize;
174 AI_InformerData.shmHeader->zonesAllocated ++;
175 ftruncate(AI_InformerData.shmFd, AI_InformerData.shmHeader->shmSize * AesalonPageSize);
177 sem_post(&AI_InformerData.shmHeader->resizeSemaphore);
180 uint32_t i;
181 for(i = 0; i < AI_InformerData.shmHeader->zonesAllocated; i ++) {
182 if(AI_ZoneAvailable(i)) break;
184 AI_MarkZone(i);
185 AI_Zone = mmap(NULL,
186 (AI_InformerData.shmHeader->zonePageOffset + i*AI_InformerData.shmHeader->zoneSize)*AesalonPageSize,
187 PROT_READ | PROT_WRITE, MAP_SHARED, AI_InformerData.shmFd,
188 AI_InformerData.shmHeader->zoneSize*AesalonPageSize);
190 ((ZoneHeader_t *)AI_Zone)->head = ((ZoneHeader_t *)AI_Zone)->tail = ZoneDataOffset;
191 ((ZoneHeader_t *)AI_Zone)->overflow = 0;
192 ((ZoneHeader_t *)AI_Zone)->processID = getpid();
193 ((ZoneHeader_t *)AI_Zone)->threadID = pthread_self();
194 sem_init(&((ZoneHeader_t *)AI_Zone)->packetSemaphore, 1, 0);
195 sem_init(&((ZoneHeader_t *)AI_Zone)->overflowSemaphore, 1, 0);
198 static int AI_ZoneAvailable(uint32_t id) {
199 uint32_t byteOffset = id / 8;
200 uint32_t bitOffset = id % 8;
201 uint32_t mask = 0x01;
202 return AI_InformerData.zoneUseData[byteOffset] & (mask << bitOffset);
205 static void AI_MarkZone(uint32_t id) {
206 uint32_t byteOffset = id / 8;
207 uint32_t bitOffset = id % 8;
208 uint32_t mask = 0x01;
209 AI_InformerData.zoneUseData[byteOffset] |= (mask << bitOffset);
212 static void AI_ClearZone(uint32_t id) {
213 uint32_t byteOffset = id / 8;
214 uint32_t bitOffset = id % 8;
215 uint32_t mask = 0x01;
216 AI_InformerData.zoneUseData[byteOffset] &= ~(mask << bitOffset);
219 static uint32_t AI_RemainingSpace() {
220 ZoneHeader_t *header = (ZoneHeader_t *)AI_Zone;
221 if(header->head <= header->tail) {
222 return ((AI_InformerData.shmHeader->zoneSize*AesalonPageSize) - ZoneDataOffset)
223 - (header->tail - header->head);
225 else {
226 return header->head - ZoneDataOffset - header->tail;
230 static void *AI_ReserveSpace(uint32_t amount) {
231 uint32_t remaining = AI_RemainingSpace();
232 ZoneHeader_t *header = (ZoneHeader_t *)AI_Zone;
233 /*uint32_t zoneDataSize = (AI_InformerData.shmHeader->zoneSize*AesalonPageSize) - ZoneDataOffset;*/
234 if(remaining < amount) {
235 header->overflow = amount - remaining;
236 sem_wait(&header->overflowSemaphore);
239 /* If the head is less than (or equal to) the tail, then the used memory
240 is in one contiguous chunk, and the buffer has not wrapped yet. */
241 if(header->tail <= header->head) {
242 /* if(header->head + amount >= zoneDataSize) {
243 header->gapSize = (amount + header->head) - zoneDataSize;
244 amount += header->gapSize;
245 header->head = ZoneDataOffset;
248 header->head += amount;
249 return &AI_Zone[header->head-amount];
251 else {
252 return NULL;
256 void AI_StartPacket(ModuleID moduleID) {
257 if(AI_Zone == NULL) AI_SetupZone();
258 AI_ZonePacket = AI_ReserveSpace(sizeof(SHMPacketHeader_t));
259 AI_ZonePacket->packetSize = 0;
260 AI_ZonePacket->moduleID = moduleID;
263 void AC_EXPORT *AI_PacketSpace(uint32_t size) {
264 AI_ZonePacket->packetSize += size;
265 return AI_ReserveSpace(size);
268 void AC_EXPORT AI_EndPacket() {
269 AI_ZonePacket = NULL;
270 sem_post(&((ZoneHeader_t *)AI_Zone)->packetSemaphore);
273 uint64_t AI_Timestamp() {
274 struct timespec t;
275 clock_gettime(CLOCK_REALTIME, &t);
276 return (t.tv_sec * 1000000000) + t.tv_nsec;
279 const char *AI_ConfigurationString(const char *name) {
280 uint32_t offset = 0;
281 while(1) {
282 const char *itemName = &AI_InformerData.configData[offset];
283 if(itemName == 0 || itemName[0] == 0) break;
285 int nameLength = strlen(itemName)+1;
286 const char *itemData = &AI_InformerData.configData[offset+nameLength];
287 if(!strcmp(name, itemName)) return itemData;
289 int dataLength = strlen(itemData)+1;
290 offset += nameLength + dataLength;
292 return NULL;
295 long AI_ConfigurationLong(const char *name) {
296 const char *s = AI_ConfigurationString(name);
297 if(s == NULL) return -1;
298 long value;
299 sscanf(s, "%ld", &value);
300 return value;
303 int AI_ConfigurationBool(const char *name) {
304 const char *s = AI_ConfigurationString(name);
305 if(s == NULL) return 0;
306 return strcmp(s, "false") != 0;
309 pthread_t *AI_TargetThreadList(int *size) {
310 if(size == NULL) return NULL;
311 *size = AI_InformerData.threadCount;
313 return AI_InformerData.threadList;
316 short AI_CollectionStatus() {
317 if(AI_InformerData.threadList == NULL) return 0;
318 pthread_t self = pthread_self();
319 int i = 0;
320 while(i < AI_InformerData.monitorThreadListSize) {
321 if(pthread_equal(self, AI_InformerData.monitorThreadList[i])) return 0;
322 i ++;
325 return 1;
328 void AI_StopCollection(pthread_t tid) {
329 if(AI_InformerData.monitorThreadListSize < AesalonInformerMonitorThreadListSize) {
330 AI_InformerData.monitorThreadList[AI_InformerData.monitorThreadListSize] = tid;
331 AI_InformerData.monitorThreadListSize ++;
333 else {
334 fprintf(stderr,
335 "Too many threads in monitor thread list, output data will be corrupted with Aesalon"
336 "collection-thread data.\n");
337 fprintf(stderr, "Increasing threadListSize in build/config may be a good idea.\n");
341 void AI_ContinueCollection(pthread_t tid) {
342 int i = 0;
343 while(i < AI_InformerData.monitorThreadListSize) {
344 if(pthread_equal(AI_InformerData.monitorThreadList[i], tid)) {
345 AI_InformerData.monitorThreadListSize --;
346 AI_InformerData.monitorThreadList[i] =
347 AI_InformerData.monitorThreadList[AI_InformerData.monitorThreadListSize-1];
348 break;
350 i ++;