Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 5d5b7a5f0f
Fetching contributors…

Cannot retrieve contributors at this time

104 lines (81 sloc) 1.847 kb
/* Measure latency of IPC using shm */
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
int main(void)
{
int pfds[2];
char c;
int shmid;
key_t key;
struct timespec *shm;
struct timespec start, stop;
int64_t delta;
int64_t max = 0;
int64_t min = INT64_MAX;
int64_t sum = 0;
int64_t count = 0;
/*
* We'll name our shared memory segment
* "5678".
*/
key = 5678;
if (!fork()) {
/*
* Create the segment.
*/
if ((shmid = shmget(key, 100, IPC_CREAT | 0666)) < 0) {
perror("shmget");
exit(1);
}
/*
* Now we attach the segment to our data space.
*/
if ((shm = shmat(shmid, NULL, 0)) == (struct timespec*) -1) {
perror("shmat");
exit(1);
}
while (1) {
clock_gettime(CLOCK_MONOTONIC, shm);
usleep(10000);
}
} else {
sleep(1);
/*
* Locate the segment.
*/
if ((shmid = shmget(key, 100, 0666)) < 0) {
perror("shmget");
exit(1);
}
/*
* Now we attach the segment to our data space.
*/
if ((shm = shmat(shmid, NULL, 0)) == (struct timespec *) -1) {
perror("shmat");
exit(1);
}
while (1) {
while ((shm->tv_sec == start.tv_sec) && (shm->tv_nsec == start.tv_nsec)) {}
clock_gettime(CLOCK_MONOTONIC, &stop);
start = *shm;
delta = ((stop.tv_sec - start.tv_sec) * (int64_t) 1000000000 +
stop.tv_nsec - start.tv_nsec);
if (delta > max)
max = delta;
else if (delta < min)
min = delta;
sum += delta;
count++;
if (!(count % 100)) {
printf("%lli %lli %lli\n", max, min, sum / count);
}
}
}
return 0;
}
Jump to Line
Something went wrong with that request. Please try again.