From ad48749eba3c88c2d75b4ec1cecfd7c2811dae60 Mon Sep 17 00:00:00 2001 From: Ethereal Date: Sat, 8 Jan 2011 22:44:43 -0700 Subject: [PATCH] SHM/zone-based communication between the monitor and informer works now. Of course, I have not actually stress-tested it yet, but one packet can now be sent and recieved successfully. I have a niggling feeling that the wrap/gap code will not work first-time, however. --- modules/informer/src/collector/informer.c | 57 ++++++++++--------------------- monitor/src/program/SharedMemory.cpp | 3 ++ monitor/src/program/ZoneReader.cpp | 1 + 3 files changed, 22 insertions(+), 39 deletions(-) diff --git a/modules/informer/src/collector/informer.c b/modules/informer/src/collector/informer.c index ae4ee19..930095c 100644 --- a/modules/informer/src/collector/informer.c +++ b/modules/informer/src/collector/informer.c @@ -135,14 +135,8 @@ void __attribute__((constructor)) AI_Construct() { AI_ContinueCollection(self); - printf("ID#: \"%s\"\n", AI_ConfigurationString("informer:moduleID")); - /*printf("data: %i\n", AI_InformerData.zoneUseData[0]);*/ - - printf("Starting packet . . .\n"); AI_StartPacket(0); - printf("Reserving space . . .\n"); AI_PacketSpace(32); - printf("Ending packet . . .\n"); AI_EndPacket(); } @@ -162,10 +156,6 @@ static void AI_SetupHeader() { static void AI_SetupConfig() { AI_InformerData.configData = mmap(NULL, AI_InformerData.shmHeader->configDataSize*AesalonPageSize, PROT_READ | PROT_WRITE, MAP_SHARED, AI_InformerData.shmFd, AesalonPageSize); - - printf("config size: %x\n", AI_InformerData.shmHeader->configDataSize*AesalonPageSize); - printf("config FD: %i\n", AI_InformerData.shmFd); - printf("config offset: %x\n", AesalonPageSize); } static void AI_SetupZoneUse() { @@ -173,24 +163,6 @@ static void AI_SetupZoneUse() { PROT_READ | PROT_WRITE, MAP_SHARED, AI_InformerData.shmFd, (AI_InformerData.shmHeader->configDataSize + 1)*AesalonPageSize); - printf("Total SHM size: %x\n", lseek(AI_InformerData.shmFd, 0, SEEK_END)); - - char content[10240]; - - int fd = open("/proc/self/maps", O_RDONLY); - - read(fd, content, sizeof(content)); - - close(fd); - - write(STDOUT_FILENO, content, strlen(content)); - - printf("zoneUseData: %p\n", AI_InformerData.zoneUseData); - printf("\tsize: %x\n", AI_InformerData.shmHeader->zoneUsagePages*AesalonPageSize); - printf("\toffset: %x\n", (AI_InformerData.shmHeader->configDataSize + 1)*AesalonPageSize); - printf("\tfd: %i\n", AI_InformerData.shmFd); - printf("\tfirst byte: %p\n", &AI_InformerData.zoneUseData[0]); - /* +1 for the header. */ AI_InformerData.shmHeader->zonePageOffset = AI_InformerData.shmHeader->zoneUsagePages + AI_InformerData.shmHeader->configDataSize + 1; @@ -199,7 +171,6 @@ static void AI_SetupZoneUse() { static void AI_SetupZone() { /* Check if more memory is required. */ while(AI_InformerData.shmHeader->zoneCount >= AI_InformerData.shmHeader->zonesAllocated) { - printf("Allocating more memory . . .\n"); /* Allocate more memory. */ sem_wait(&AI_InformerData.shmHeader->resizeSemaphore); if(AI_InformerData.shmHeader->zoneCount >= AI_InformerData.shmHeader->zonesAllocated) { @@ -210,24 +181,26 @@ static void AI_SetupZone() { sem_post(&AI_InformerData.shmHeader->resizeSemaphore); } - printf("Looking for zone . . . (allocated zones: %i)\n", AI_InformerData.shmHeader->zonesAllocated); - uint32_t i; for(i = 0; i < AI_InformerData.shmHeader->zonesAllocated; i ++) { - printf("looping . . .\n"); if(AI_ZoneAvailable(i)) break; } - printf("Zone ID: %i\n", i); + if(i == AI_InformerData.shmHeader->zonesAllocated) { + /* Something went pretty seriously wrong. Perhaps another target jumped in and took the spot first? */ + printf("Something very wrong occurred. Trying again . . .\n"); + AI_SetupZone(); + } AI_MarkZone(i); AI_Zone = mmap(NULL, - (AI_InformerData.shmHeader->zonePageOffset + i*AI_InformerData.shmHeader->zoneSize)*AesalonPageSize, + AI_InformerData.shmHeader->zoneSize*AesalonPageSize, PROT_READ | PROT_WRITE, MAP_SHARED, AI_InformerData.shmFd, - AI_InformerData.shmHeader->zoneSize*AesalonPageSize); + (AI_InformerData.shmHeader->zonePageOffset + i*AI_InformerData.shmHeader->zoneSize)*AesalonPageSize); ((ZoneHeader_t *)AI_Zone)->head = ((ZoneHeader_t *)AI_Zone)->tail = ZoneDataOffset; ((ZoneHeader_t *)AI_Zone)->overflow = 0; ((ZoneHeader_t *)AI_Zone)->processID = getpid(); ((ZoneHeader_t *)AI_Zone)->threadID = pthread_self(); + sem_init(&((ZoneHeader_t *)AI_Zone)->packetSemaphore, 1, 0); sem_init(&((ZoneHeader_t *)AI_Zone)->overflowSemaphore, 1, 0); } @@ -236,7 +209,7 @@ static int AI_ZoneAvailable(uint32_t id) { uint32_t byteOffset = id / 8; uint32_t bitOffset = id % 8; uint32_t mask = 0x01; - return AI_InformerData.zoneUseData[byteOffset] & (mask << bitOffset); + return !(AI_InformerData.zoneUseData[byteOffset] & (mask << bitOffset)); } static void AI_MarkZone(uint32_t id) { @@ -282,8 +255,8 @@ static void *AI_ReserveSpace(uint32_t amount) { header->head = ZoneDataOffset; }*/ - header->head += amount; - return &AI_Zone[header->head-amount]; + header->tail += amount; + return &AI_Zone[header->tail-amount]; } else { return NULL; @@ -303,8 +276,14 @@ void AC_EXPORT *AI_PacketSpace(uint32_t size) { } void AC_EXPORT AI_EndPacket() { + printf("packet size: %i\n", AI_ZonePacket->packetSize); AI_ZonePacket = NULL; - sem_post(&((ZoneHeader_t *)AI_Zone)->packetSemaphore); + + ZoneHeader_t *header = (ZoneHeader_t *)AI_Zone; + + sem_post(&header->packetSemaphore); + + sem_post(&AI_InformerData.shmHeader->packetSemaphore); } uint64_t AI_Timestamp() { diff --git a/monitor/src/program/SharedMemory.cpp b/monitor/src/program/SharedMemory.cpp index c2eef60..b5ff1f6 100644 --- a/monitor/src/program/SharedMemory.cpp +++ b/monitor/src/program/SharedMemory.cpp @@ -50,7 +50,9 @@ uint8_t *SharedMemory::zoneWithPacket() { if(m_zoneUseData[i] & (0x01 << (i % 8))) { uint8_t *zoneData = zone(i); ZoneHeader_t *zoneHeader = reinterpret_cast(zoneData); + if(sem_trywait(&zoneHeader->packetSemaphore) == -1 && errno == EAGAIN) continue; + return zoneData; } } @@ -77,6 +79,7 @@ uint8_t *SharedMemory::zone(uint32_t id) { data = static_cast(mmap(NULL, AesalonPageSize*m_header->zoneSize, PROT_READ | PROT_WRITE, MAP_SHARED, m_fd, (m_header->zonePageOffset + id*m_header->zoneSize)*AesalonPageSize)); + m_zoneMap[id] = data; return data; } diff --git a/monitor/src/program/ZoneReader.cpp b/monitor/src/program/ZoneReader.cpp index b6c9a76..ce0aa3f 100644 --- a/monitor/src/program/ZoneReader.cpp +++ b/monitor/src/program/ZoneReader.cpp @@ -50,6 +50,7 @@ void *ZoneReader::run(void *voidInstance) { ZoneReader *instance = static_cast(voidInstance); while(true) { instance->m_sharedMemory->waitForPacket(); + uint8_t *zoneData = instance->m_sharedMemory->zoneWithPacket(); if(zoneData == NULL) continue; -- 2.11.4.GIT