2 * Copyright (C) 2012 Pierre Habouzit <pierre.habouzit@intersec.com>
3 * Copyright (C) 2012 Intersec SAS
5 * This file is part of the Linux Pthread Workqueue Regulator tests.
7 * The Linux Pthread Workqueue Regulator is free software: you can
8 * redistribute it and/or modify it under the terms of the GNU Lesser
9 * General Public License as published by the Free Software Foundation,
10 * either version 2.1 of the License, or (at your option) any later version.
12 * The Linux Pthread Workqueue Regulator is distributed in the hope that it
13 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
14 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with The Linux Pthread Workqueue Regultaor.
19 * If not, see <http://www.gnu.org/licenses/>.
30 #include <sys/ioctl.h>
33 #include <sys/types.h>
36 #include "../kernel/pwqr.h"
38 /* pwqr wrapping {{{ */
40 static int pwqr_create(int flags)
42 if (flags & ~PWQR_FL__SET) {
46 return open("/dev/"PWQR_DEVICE_NAME, O_RDWR | flags);
49 static int pwqr_ctl(int fd, int op, int val, void *uaddr)
51 struct pwqr_ioc_wait wait;
54 case PWQR_CTL_GET_CONC:
55 case PWQR_CTL_REGISTER:
56 case PWQR_CTL_UNREGISTER:
59 case PWQR_CTL_SET_CONC:
61 case PWQR_CTL_WAKE_OC:
62 return ioctl(fd, op, val);
64 wait.pwqr_ticket = val;
65 wait.pwqr_uaddr = uaddr;
66 return ioctl(fd, op, &wait);
75 static int pwqr_fd_g = -1;
78 static void *thr_main(void *unused)
80 if (pwqr_ctl(pwqr_fd_g, PWQR_CTL_REGISTER, 0, NULL) < 0)
81 err(-1, "pwqr_ctl(PWQR_CTL_REGISTER)");
89 static uint64_t tv_diff(const struct timeval *t1, const struct timeval *t2)
91 return (t1->tv_sec - t2->tv_sec) * 1000000ULL + t1->tv_usec - t2->tv_usec;
98 struct timeval start, tv;
101 pwqr_fd_g = pwqr_create(PWQR_FL_NONBLOCK);
103 err(-1, "pwqr_create");
105 pi_g = pwqr_ctl(pwqr_fd_g, PWQR_CTL_GET_CONC, 0, NULL);
107 err(-1, "pwqr_ctl(PWQR_CTL_GET_CONC)");
109 for (int i = 0; i < pi_g + 1; i++) {
110 pthread_create(&tids[i], NULL, thr_main, NULL);
115 gettimeofday(&start, NULL);
118 rc = poll(&pfd, 1, -1);
124 if (pfd.revents & POLLIN) {
125 if (read(pwqr_fd_g, &rc, sizeof(rc)) != sizeof(rc)) {
128 gettimeofday(&tv, NULL);
129 warnx("[%06jd.%06jd] overcommit of %d",
130 tv_diff(&tv, &start) / 1000000,
131 tv_diff(&tv, &start) % 1000000, rc);
134 if (pfd.revents & POLLHUP) {
135 errx(-1, "poll returned POLLHUP");
140 for (int i = 0; i < pi_g + 1; i++) {
141 pthread_join(tids[i], NULL);