File: rbwriter.c

package info (click to toggle)
libqb 0.11.1-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 2,804 kB
  • ctags: 1,689
  • sloc: ansic: 15,630; sh: 11,355; makefile: 271
file content (170 lines) | stat: -rw-r--r-- 3,883 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * Copyright (c) 2009 Red Hat, Inc.
 *
 * All rights reserved.
 *
 * Author: Steven Dake (sdake@redhat.com)
 *
 * libqb is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * libqb is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with libqb.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <qb/qbipcc.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>

#include <qb/qbrb.h>
#include <qb/qbdefs.h>
#include <qb/qbutil.h>
#include <qb/qblog.h>

static qb_ringbuffer_t *rb = NULL;
#define ITERATIONS 100000
#define BUFFER_CHUNK_SIZE (50*50*10)

static struct timeval tv1, tv2, tv_elapsed;

#define timersub(a, b, result)					\
do {								\
	(result)->tv_sec = (a)->tv_sec - (b)->tv_sec;		\
	(result)->tv_usec = (a)->tv_usec - (b)->tv_usec;	\
	if ((result)->tv_usec < 0) {				\
		--(result)->tv_sec;				\
		(result)->tv_usec += 1000000;			\
	}							\
} while (0)

static void sigterm_handler(int32_t num)
{
	qb_log(LOG_INFO, "writer: %s(%d)\n", __func__, num);
	qb_rb_close(rb);
	exit(0);
}

static void bm_start(void)
{
	gettimeofday(&tv1, NULL);
}

static void bm_finish(const char *operation, int32_t size)
{
	float ops_per_sec;
	float mbs_per_sec;

	gettimeofday(&tv2, NULL);
	timersub(&tv2, &tv1, &tv_elapsed);

	ops_per_sec =
	    ((float)ITERATIONS) / (((float)tv_elapsed.tv_sec) +
				   (((float)tv_elapsed.tv_usec) / 1000000.0));

	mbs_per_sec =
	    ((((float)ITERATIONS) * size) /
	     (((float)tv_elapsed.tv_sec) +
	      (((float)tv_elapsed.tv_usec) / 1000000.0))) / (1024.0 * 1024.0);

	qb_log(LOG_INFO, "write size %d OPs/sec %9.3f MB/sec %9.3f",
	       size, ops_per_sec, mbs_per_sec);
}

static void bmc_connect(void)
{
	rb = qb_rb_open("tester", BUFFER_CHUNK_SIZE * 3,
			QB_RB_FLAG_SHARED_PROCESS, 0);

	if (rb == NULL) {
		qb_perror(LOG_ERR, "failed to create ringbuffer");
		exit(1);
	}

}

static char buffer[1024 * 1024];
static void bmc_send_nozc(size_t size)
{
	ssize_t res = 0;

repeat_send:
	res = qb_rb_chunk_write(rb, buffer, size);
	if (res < size) {
		goto repeat_send;
	}
}

static void show_usage(const char *name)
{
	printf("usage: \n");
	printf("%s <options>\n", name);
	printf("\n");
	printf("  options:\n");
	printf("\n");
	printf("  -n             non-blocking ipc (default blocking)\n");
	printf("  -v             verbose\n");
	printf("  -h             show this help text\n");
	printf("\n");
}

int32_t main(int32_t argc, char *argv[])
{
	const char *options = "vh";
	int32_t opt;
	int32_t i, j;
	int32_t size;
	int32_t verbose = 0;

	while ((opt = getopt(argc, argv, options)) != -1) {
		switch (opt) {
		case 'v':
			verbose++;
			break;
		case 'h':
		default:
			show_usage(argv[0]);
			exit(0);
			break;
		}
	}

	signal(SIGINT, sigterm_handler);

	qb_log_init("rbwriter", LOG_USER, LOG_EMERG);
	qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_FALSE);
	qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_ADD,
			  QB_LOG_FILTER_FILE, "*", LOG_INFO + verbose);
	qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_TRUE);

	bmc_connect();

	for (j = 1; j < 49; j++) {
		bm_start();
		size = 7 * (j + 1) * j;

		if (size > BUFFER_CHUNK_SIZE) {
			size = BUFFER_CHUNK_SIZE;
		}

		for (i = 0; i < ITERATIONS; i++) {
			bmc_send_nozc(size);
		}
		bm_finish("ringbuffer", size);
	}
	qb_rb_close(rb);
	return EXIT_SUCCESS;
}