Skip to content

Commit

Permalink
Additional cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
rigtorp committed Feb 23, 2016
1 parent 1170563 commit 1e2886d
Show file tree
Hide file tree
Showing 12 changed files with 203 additions and 157 deletions.
22 changes: 22 additions & 0 deletions LICENSE
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2016 Erik Rigtorp <erik@rigtorp.se>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

2 changes: 1 addition & 1 deletion Makefile
@@ -1,5 +1,5 @@

CFLAGS = -g
CFLAGS = -g -Wall -O3

all: pipe_lat pipe_thr \
unix_lat unix_thr \
Expand Down
7 changes: 6 additions & 1 deletion README.md
Expand Up @@ -20,4 +20,9 @@ The shared memory benchmark is a kind of "control". If run under a
real-time OS it will give you the intra core communication
latency.

This software is distributed under the MIT License.
This software is distributed under the MIT License.

Credits
-------

* *desbma* for adding cross platform support for clock_gettime
26 changes: 11 additions & 15 deletions pipe_lat.c
Expand Up @@ -2,7 +2,7 @@
Measure latency of IPC using unix domain sockets
Copyright (c) 2010 Erik Rigtorp <erik@rigtorp.com>
Copyright (c) 2016 Erik Rigtorp <erik@rigtorp.se>
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
Expand All @@ -26,21 +26,19 @@
OTHER DEALINGS IN THE SOFTWARE.
*/


#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <time.h>
#include <stdint.h>
#include <unistd.h>

#if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) && defined(_POSIX_MONOTONIC_CLOCK)
#if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) && \
defined(_POSIX_MONOTONIC_CLOCK)
#define HAS_CLOCK_GETTIME_MONOTONIC
#endif


int main(int argc, char *argv[])
{
int main(int argc, char *argv[]) {
int ofds[2];
int ifds[2];

Expand All @@ -54,7 +52,7 @@ int main(int argc, char *argv[])
#endif

if (argc != 3) {
printf ("usage: pipe_lat <message-size> <roundtrip-count>\n");
printf("usage: pipe_lat <message-size> <roundtrip-count>\n");
return 1;
}

Expand All @@ -68,7 +66,7 @@ int main(int argc, char *argv[])
}

printf("message size: %i octets\n", size);
printf("roundtrip count: %lli\n", count);
printf("roundtrip count: %li\n", count);

if (pipe(ofds) == -1) {
perror("pipe");
Expand All @@ -80,7 +78,7 @@ int main(int argc, char *argv[])
return 1;
}

if (!fork()) { /* child */
if (!fork()) { /* child */
for (i = 0; i < count; i++) {

if (read(ifds[0], buf, size) != size) {
Expand Down Expand Up @@ -118,7 +116,6 @@ int main(int argc, char *argv[])
perror("read");
return 1;
}

}

#ifdef HAS_CLOCK_GETTIME_MONOTONIC
Expand All @@ -136,13 +133,12 @@ int main(int argc, char *argv[])
return 1;
}

delta = (stop.tv_sec - start.tv_sec) * 1000000 +
(stop.tv_usec - start.tv_usec);
delta =
(stop.tv_sec - start.tv_sec) * 1000000 + (stop.tv_usec - start.tv_usec);

#endif

printf("average latency: %lli us\n", delta / (count * 2));

printf("average latency: %li us\n", delta / (count * 2));
}

return 0;
Expand Down
27 changes: 13 additions & 14 deletions pipe_thr.c
Expand Up @@ -2,7 +2,7 @@
Measure throughput of IPC using pipes
Copyright (c) 2010 Erik Rigtorp <erik@rigtorp.com>
Copyright (c) 2016 Erik Rigtorp <erik@rigtorp.se>
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
Expand All @@ -26,21 +26,19 @@
OTHER DEALINGS IN THE SOFTWARE.
*/


#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <time.h>
#include <stdint.h>
#include <unistd.h>

#if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) && defined(_POSIX_MONOTONIC_CLOCK)
#if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) && \
defined(_POSIX_MONOTONIC_CLOCK)
#define HAS_CLOCK_GETTIME_MONOTONIC
#endif


int main(int argc, char *argv[])
{
int main(int argc, char *argv[]) {
int fds[2];

int size;
Expand All @@ -53,7 +51,7 @@ int main(int argc, char *argv[])
#endif

if (argc != 3) {
printf ("usage: pipe_thr <message-size> <message-count>\n");
printf("usage: pipe_thr <message-size> <message-count>\n");
return 1;
}

Expand All @@ -67,7 +65,7 @@ int main(int argc, char *argv[])
}

printf("message size: %i octets\n", size);
printf("message count: %lli\n", count);
printf("message count: %li\n", count);

if (pipe(fds) == -1) {
perror("pipe");
Expand All @@ -84,7 +82,7 @@ int main(int argc, char *argv[])
}
}
} else {
/* parent */
/* parent */

#ifdef HAS_CLOCK_GETTIME_MONOTONIC
if (clock_gettime(CLOCK_MONOTONIC, &start) == -1) {
Expand Down Expand Up @@ -120,13 +118,14 @@ int main(int argc, char *argv[])
return 1;
}

delta = (stop.tv_sec - start.tv_sec) * 1000000 +
(stop.tv_usec - start.tv_usec);
delta =
(stop.tv_sec - start.tv_sec) * 1000000 + (stop.tv_usec - start.tv_usec);

#endif

printf("average throughput: %lli msg/s\n", (count * 1000000) / delta);
printf("average throughput: %lli Mb/s\n", (((count * 1000000) / delta) * size * 8) / 1000000);
printf("average throughput: %li msg/s\n", (count * 1000000) / delta);
printf("average throughput: %li Mb/s\n",
(((count * 1000000) / delta) * size * 8) / 1000000);
}

return 0;
Expand Down
55 changes: 41 additions & 14 deletions shm.c
@@ -1,20 +1,46 @@
/* Measure latency of IPC using shm */
/*
Measure latency of IPC using shm
Copyright (c) 2016 Erik Rigtorp <erik@rigtorp.se>
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>

int main(void)
{
int main(void) {
int shmid;
key_t key;
struct timespec *shm;
struct timeval *shm;

struct timespec start, stop;
struct timeval start, stop;

int64_t delta;

Expand Down Expand Up @@ -42,7 +68,7 @@ int main(void)
/*
* Now we attach the segment to our data space.
*/
if ((shm = shmat(shmid, NULL, 0)) == (struct timespec*) -1) {
if ((shm = shmat(shmid, NULL, 0)) == (struct timeval *)-1) {
perror("shmat");
return 1;
}
Expand All @@ -66,18 +92,19 @@ int main(void)
/*
* Now we attach the segment to our data space.
*/
if ((shm = shmat(shmid, NULL, 0)) == (struct timespec *) -1) {
if ((shm = shmat(shmid, NULL, 0)) == (struct timeval *)-1) {
perror("shmat");
return 1;
}

while (1) {
while ((shm->tv_sec == start.tv_sec) && (shm->tv_nsec == start.tv_nsec)) {}
while ((shm->tv_sec == start.tv_sec) && (shm->tv_usec == start.tv_usec)) {
}

gettimeofday(&stop, NULL);
start = *shm;
delta = ((stop.tv_sec - start.tv_sec) * (int64_t) 1000000000 +
stop.tv_nsec - start.tv_nsec);
delta = ((stop.tv_sec - start.tv_sec) * 1000000 + stop.tv_usec -
start.tv_usec);

if (delta > max)
max = delta;
Expand All @@ -88,7 +115,7 @@ int main(void)
count++;

if (!(count % 100)) {
printf("%lli %lli %lli\n", max, min, sum / count);
printf("%li %li %li\n", max, min, sum / count);
}
}
}
Expand Down

0 comments on commit 1e2886d

Please sign in to comment.