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
19 #include <sys/types.h>
25 #include "informer/Informer.h"
26 #include "common/Config.h"
27 #include "common/ZoneHeader.h"
29 typedef struct Zone_t Zone_t
;
35 typedef struct InformerData_t InformerData_t
;
36 struct InformerData_t
{
40 pthread_t monitorThreadList
[AesalonInformerMonitorThreadListSize
];
41 int monitorThreadListSize
;
43 pthread_t
*threadList
;
49 SharedMemoryHeader_t
*shmHeader
;
51 const char *configData
;
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
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
);
100 int fd
= open("/proc/self/cmdline", O_RDONLY
);
102 read(fd
, filename
, sizeof(filename
));
106 /* String hashing algorithm: djb2. */
108 uint64_t pathHash
= 0;
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. */
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");
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
);
143 void __attribute__((destructor
)) AI_Destruct() {
144 printf("[AI] Destructing Informer . . .\n");
147 static void AI_OpenSHM(const char *name
) {
148 AI_InformerData
.shmFd
= shm_open(name
, O_RDWR
, S_IRUSR
| S_IWUSR
);
151 static void AI_SetupHeader() {
152 AI_InformerData
.shmHeader
= mmap(NULL
, AesalonPageSize
,
153 PROT_READ
| PROT_WRITE
, MAP_SHARED
, AI_InformerData
.shmFd
, 0);
156 static void AI_SetupConfig() {
157 AI_InformerData
.configData
= mmap(NULL
, AI_InformerData
.shmHeader
->configDataSize
*AesalonPageSize
,
158 PROT_READ
| PROT_WRITE
, MAP_SHARED
, AI_InformerData
.shmFd
, AesalonPageSize
);
161 static void AI_SetupZoneUse() {
162 AI_InformerData
.zoneUseData
= mmap(NULL
, AI_InformerData
.shmHeader
->zoneUsagePages
*AesalonPageSize
,
163 PROT_READ
| PROT_WRITE
, MAP_SHARED
, AI_InformerData
.shmFd
,
164 (AI_InformerData
.shmHeader
->configDataSize
+ 1)*AesalonPageSize
);
166 /* +1 for the header. */
167 AI_InformerData
.shmHeader
->zonePageOffset
=
168 AI_InformerData
.shmHeader
->zoneUsagePages
+ AI_InformerData
.shmHeader
->configDataSize
+ 1;
171 static void AI_SetupZone() {
172 /* Check if more memory is required. */
173 while(AI_InformerData
.shmHeader
->zoneCount
>= AI_InformerData
.shmHeader
->zonesAllocated
) {
174 /* Allocate more memory. */
175 sem_wait(&AI_InformerData
.shmHeader
->resizeSemaphore
);
176 if(AI_InformerData
.shmHeader
->zoneCount
>= AI_InformerData
.shmHeader
->zonesAllocated
) {
177 AI_InformerData
.shmHeader
->shmSize
+= AI_InformerData
.shmHeader
->zoneSize
;
178 AI_InformerData
.shmHeader
->zonesAllocated
++;
179 ftruncate(AI_InformerData
.shmFd
, AI_InformerData
.shmHeader
->shmSize
* AesalonPageSize
);
181 sem_post(&AI_InformerData
.shmHeader
->resizeSemaphore
);
185 for(i
= 0; i
< AI_InformerData
.shmHeader
->zonesAllocated
; i
++) {
186 if(AI_ZoneAvailable(i
)) break;
188 if(i
== AI_InformerData
.shmHeader
->zonesAllocated
) {
189 /* Something went pretty seriously wrong. Perhaps another target jumped in and took the spot first? */
190 printf("Something very wrong occurred. Trying again . . .\n");
195 AI_InformerData
.shmHeader
->zoneSize
*AesalonPageSize
,
196 PROT_READ
| PROT_WRITE
, MAP_SHARED
, AI_InformerData
.shmFd
,
197 (AI_InformerData
.shmHeader
->zonePageOffset
+ i
*AI_InformerData
.shmHeader
->zoneSize
)*AesalonPageSize
);
199 ((ZoneHeader_t
*)AI_Zone
)->head
= ((ZoneHeader_t
*)AI_Zone
)->tail
= ZoneDataOffset
;
200 ((ZoneHeader_t
*)AI_Zone
)->overflow
= 0;
201 ((ZoneHeader_t
*)AI_Zone
)->processID
= getpid();
202 ((ZoneHeader_t
*)AI_Zone
)->threadID
= pthread_self();
204 sem_init(&((ZoneHeader_t
*)AI_Zone
)->packetSemaphore
, 1, 0);
205 sem_init(&((ZoneHeader_t
*)AI_Zone
)->overflowSemaphore
, 1, 0);
208 static int AI_ZoneAvailable(uint32_t id
) {
209 uint32_t byteOffset
= id
/ 8;
210 uint32_t bitOffset
= id
% 8;
211 uint32_t mask
= 0x01;
212 return !(AI_InformerData
.zoneUseData
[byteOffset
] & (mask
<< bitOffset
));
215 static void AI_MarkZone(uint32_t id
) {
216 uint32_t byteOffset
= id
/ 8;
217 uint32_t bitOffset
= id
% 8;
218 uint32_t mask
= 0x01;
219 AI_InformerData
.zoneUseData
[byteOffset
] |= (mask
<< bitOffset
);
222 static void AI_ClearZone(uint32_t id
) {
223 uint32_t byteOffset
= id
/ 8;
224 uint32_t bitOffset
= id
% 8;
225 uint32_t mask
= 0x01;
226 AI_InformerData
.zoneUseData
[byteOffset
] &= ~(mask
<< bitOffset
);
229 static uint32_t AI_RemainingSpace() {
230 ZoneHeader_t
*header
= (ZoneHeader_t
*)AI_Zone
;
231 if(header
->head
<= header
->tail
) {
232 return ((AI_InformerData
.shmHeader
->zoneSize
*AesalonPageSize
) - ZoneDataOffset
)
233 - (header
->tail
- header
->head
);
236 return header
->head
- ZoneDataOffset
- header
->tail
;
240 static void *AI_ReserveSpace(uint32_t amount
) {
241 uint32_t remaining
= AI_RemainingSpace();
242 ZoneHeader_t
*header
= (ZoneHeader_t
*)AI_Zone
;
243 /*uint32_t zoneDataSize = (AI_InformerData.shmHeader->zoneSize*AesalonPageSize) - ZoneDataOffset;*/
244 if(remaining
< amount
) {
245 header
->overflow
= amount
- remaining
;
246 sem_wait(&header
->overflowSemaphore
);
249 /* If the head is less than (or equal to) the tail, then the used memory
250 is in one contiguous chunk, and the buffer has not wrapped yet. */
251 if(header
->tail
<= header
->head
) {
252 /* if(header->head + amount >= zoneDataSize) {
253 header->gapSize = (amount + header->head) - zoneDataSize;
254 amount += header->gapSize;
255 header->head = ZoneDataOffset;
258 header
->tail
+= amount
;
259 return &AI_Zone
[header
->tail
-amount
];
266 void AI_StartPacket(ModuleID moduleID
) {
267 if(AI_Zone
== NULL
) AI_SetupZone();
268 AI_ZonePacket
= AI_ReserveSpace(sizeof(SHMPacketHeader_t
));
269 AI_ZonePacket
->packetSize
= 0;
270 AI_ZonePacket
->moduleID
= moduleID
;
273 void AC_EXPORT
*AI_PacketSpace(uint32_t size
) {
274 AI_ZonePacket
->packetSize
+= size
;
275 return AI_ReserveSpace(size
);
278 void AC_EXPORT
AI_EndPacket() {
279 printf("packet size: %i\n", AI_ZonePacket
->packetSize
);
280 AI_ZonePacket
= NULL
;
282 ZoneHeader_t
*header
= (ZoneHeader_t
*)AI_Zone
;
284 sem_post(&header
->packetSemaphore
);
286 sem_post(&AI_InformerData
.shmHeader
->packetSemaphore
);
289 uint64_t AI_Timestamp() {
291 clock_gettime(CLOCK_REALTIME
, &t
);
292 return (t
.tv_sec
* 1000000000) + t
.tv_nsec
;
295 const char *AI_ConfigurationString(const char *name
) {
298 const char *itemName
= &AI_InformerData
.configData
[offset
];
299 if(itemName
== 0 || itemName
[0] == 0) break;
301 int nameLength
= strlen(itemName
)+1;
302 const char *itemData
= &AI_InformerData
.configData
[offset
+nameLength
];
303 if(!strcmp(name
, itemName
)) return itemData
;
305 int dataLength
= strlen(itemData
)+1;
306 offset
+= nameLength
+ dataLength
;
311 long AI_ConfigurationLong(const char *name
) {
312 const char *s
= AI_ConfigurationString(name
);
313 if(s
== NULL
) return -1;
315 sscanf(s
, "%ld", &value
);
319 int AI_ConfigurationBool(const char *name
) {
320 const char *s
= AI_ConfigurationString(name
);
321 if(s
== NULL
) return 0;
322 return strcmp(s
, "false") != 0;
325 pthread_t
*AI_TargetThreadList(int *size
) {
326 if(size
== NULL
) return NULL
;
327 *size
= AI_InformerData
.threadCount
;
329 return AI_InformerData
.threadList
;
332 short AI_CollectionStatus() {
333 if(AI_InformerData
.threadList
== NULL
) return 0;
334 pthread_t self
= pthread_self();
336 while(i
< AI_InformerData
.monitorThreadListSize
) {
337 if(pthread_equal(self
, AI_InformerData
.monitorThreadList
[i
])) return 0;
344 void AI_StopCollection(pthread_t tid
) {
345 if(AI_InformerData
.monitorThreadListSize
< AesalonInformerMonitorThreadListSize
) {
346 AI_InformerData
.monitorThreadList
[AI_InformerData
.monitorThreadListSize
] = tid
;
347 AI_InformerData
.monitorThreadListSize
++;
351 "Too many threads in monitor thread list, output data will be corrupted with Aesalon"
352 "collection-thread data.\n");
353 fprintf(stderr
, "Increasing threadListSize in build/config may be a good idea.\n");
357 void AI_ContinueCollection(pthread_t tid
) {
359 while(i
< AI_InformerData
.monitorThreadListSize
) {
360 if(pthread_equal(AI_InformerData
.monitorThreadList
[i
], tid
)) {
361 AI_InformerData
.monitorThreadListSize
--;
362 AI_InformerData
.monitorThreadList
[i
] =
363 AI_InformerData
.monitorThreadList
[AI_InformerData
.monitorThreadListSize
-1];