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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
|
#include "Interface.h"
#include "Log.h"
#include "MiscUtils.h"
#include "ShmInterface.h"
#include "plf_nanotimer/plf_nanotimer.h"
#ifdef _WIN32
# define APPNAME "shm_test.exe"
# ifndef NOMINMAX
# define NOMINMAX
# endif
# include <windows.h>
#else
# define APPNAME "shm_test"
# include <unistd.h>
# include <stdio.h>
# include <dlfcn.h>
# include <sys/wait.h>
# include <dlfcn.h>
#endif
#include <cstring>
#include <thread>
#include <cmath>
using namespace vst;
#define TEST_REALTIME 1
#define TEST_QUEUE 1
#define TEST_QUEUE_COUNT 100
#define TEST_QUEUE_BUFSIZE 256
#define TEST_REQUEST 1
#define TEST_REQUEST_BUFSIZE 512
#define TEST_BENCHMARK 1
#define TEST_BENCHMARK_COUNT 20
#define TEST_BENCHMARK_SLEEP -1 // negative: don't sleep
#define TEST_BENCHMARK_DSP_COUNT 0
#define TEST_BENCHMARK_AVG_OFFSET 1
#define TEST_BENCHMARK_DEBUG 0
static volatile float gPhase = 0.0;
static volatile float gBuffer[64];
void fake_dsp(int n){
// a simple sine oscillator
const float advance = 440.0 / 44100.0;
float phase = gPhase;
for (int i = 0; i < n; ++i){
for (int j = 0; j < 64; ++j){
phase = fmodf(phase + advance, 1.0);
gBuffer[j] = cos(phase * 6.28318530717959); // force write
}
}
gPhase = phase;
}
void sleep_ms(int ms){
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
}
void server_test_queue(ShmInterface& shm){
LOG_INFO("---");
LOG_INFO("test request");
LOG_INFO("---");
LOG_INFO("server: queue");
auto& channel = shm.getChannel(0);
channel.clear();
LOG_INFO("server: channel " << channel.name());
for (int i = 0; i < TEST_QUEUE_COUNT; ++i){
char buf[64];
snprintf(buf, sizeof(buf), "msg %d", i+1);
if (channel.writeMessage(buf, strlen(buf) + 1)){
LOG_INFO("server: write message " << buf);
channel.post();
} else {
LOG_ERROR("server: couldn't write message " << buf);
}
sleep_ms(1); // prevent queue overflow
}
LOG_INFO("server: send quit");
const char *quit = "quit";
channel.writeMessage(quit, strlen(quit) + 1);
channel.post();
LOG_INFO("server: done");
}
void client_test_queue(ShmInterface& shm){
LOG_INFO("client: queue");
auto& channel = shm.getChannel(0);
LOG_INFO("client: channel " << channel.name());
int count = 0;
for (;;){
char buf[64];
size_t size = sizeof(buf);
while (channel.readMessage(buf, size)){
LOG_INFO("client: got message " << buf);
if (!strcmp(buf, "quit")){
LOG_INFO("---");
LOG_INFO("client: got " << count << " messages");
return;
} else {
count++;
}
}
if (size > sizeof(buf)){
LOG_ERROR("client: couldn't read message");
} else {
LOG_INFO("client: waiting for message");
}
channel.wait();
}
}
void server_test_request(ShmInterface& shm){
LOG_INFO("---");
LOG_INFO("test request");
LOG_INFO("---");
LOG_INFO("server: request");
auto& channel = shm.getChannel(1);
channel.clear();
LOG_INFO("server: channel " << channel.name());
// post message
const char* msg[] = { "testing", "shared", "memory", "interface" };
for (int i = 0; i < 4; ++i){
LOG_INFO("server: add msg: " << msg[i]);
channel.addMessage(msg[i], strlen(msg[i]) + 1);
}
LOG_INFO("server: send msg");
channel.post();
// wait for reply
LOG_INFO("server: wait for reply");
channel.waitReply();
const void *reply;
size_t replySize;
channel.getMessage(reply, replySize);
LOG_INFO("server: got reply: " << (const char *)reply);
}
void client_test_request(ShmInterface& shm){
LOG_INFO("client: request");
auto& channel = shm.getChannel(1);
LOG_INFO("client: channel " << channel.name());
// wait for messages
LOG_INFO("client: wait for message");
channel.wait();
for (int i = 0; i < 4; ++i){
const void *msg;
size_t msgSize;
channel.getMessage(msg, msgSize);
LOG_INFO("client: got message: " << (const char *)msg);
}
// post reply
auto reply = "ok";
LOG_INFO("client: send reply: " << reply);
channel.clear();
channel.addMessage(reply, strlen(reply) + 1);
channel.postReply();
}
void server_benchmark(ShmInterface& shm){
LOG_INFO("---");
LOG_INFO("test benchmark");
LOG_INFO("---");
LOG_INFO("server: benchmark");
auto& channel = shm.getChannel(1);
LOG_INFO("server: channel " << channel.name());
plf::nanotimer timer;
timer.start();
{
auto t1 = timer.get_elapsed_us();
auto t2 = timer.get_elapsed_us();
LOG_INFO("server: no sleep = " << (t2 - t1) << " us");
}
{
auto t1 = timer.get_elapsed_us();
sleep_ms(0);
auto t2 = timer.get_elapsed_us();
LOG_INFO("server: sleep(0) = " << (t2 - t1) << " us");
}
double avg_outer = 0;
double avg_inner = 0;
for (int i = 0; i < TEST_BENCHMARK_COUNT; ++i){
auto t1 = timer.get_elapsed_us();
channel.clear();
// post message
auto msg = "test";
channel.addMessage(msg, strlen(msg) + 1);
auto t2 = timer.get_elapsed_us();
#if TEST_BENCHMARK_DEBUG
LOG_INFO("server: post");
#endif
channel.post();
// wait for reply
#if TEST_BENCHMARK_DEBUG
LOG_INFO("server: wait for reply");
#endif
channel.waitReply();
auto t3 = timer.get_elapsed_us();
const void *reply;
size_t replySize;
channel.getMessage(reply, replySize);
fake_dsp(TEST_BENCHMARK_DSP_COUNT);
auto t4 = timer.get_elapsed_us();
auto outer = t4 - t1;
auto inner = t3 - t2;
if (i >= TEST_BENCHMARK_AVG_OFFSET){
avg_outer += outer;
avg_inner += inner;
}
LOG_INFO("server: full delta = " << outer << " us, "
<< "inner delta = " << inner << " us");
#if TEST_BENCHMARK_SLEEP >= 0
// make sure that child process actually has to wake up
sleep_ms(TEST_BENCHMARK_SLEEP);
#endif
}
auto divisor = TEST_BENCHMARK_COUNT - TEST_BENCHMARK_AVG_OFFSET;
LOG_INFO("---");
LOG_INFO("server: average full delta = "
<< (avg_outer / divisor) << " us");
LOG_INFO("server: average inner delta = "
<< (avg_inner / divisor) << " us");
}
void client_benchmark(ShmInterface& shm){
LOG_INFO("client: benchmark");
auto& channel = shm.getChannel(1);
LOG_INFO("client: channel " << channel.name());
for (int i = 0; i < TEST_BENCHMARK_COUNT; ++i){
// wait for message
#if TEST_BENCHMARK_DEBUG
LOG_INFO("client: wait");
#endif
channel.wait();
const void *msg;
size_t msgSize;
channel.getMessage(msg, msgSize);
// post reply
#if TEST_BENCHMARK_DEBUG
LOG_INFO("client: post reply");
#endif
auto reply = "ok";
channel.clear();
channel.addMessage(reply, strlen(reply) + 1);
channel.postReply();
}
LOG_INFO("client: done");
}
int server_run(){
LOG_INFO("---");
LOG_INFO("server: start");
LOG_INFO("---");
ShmInterface shm;
shm.addChannel(ShmChannel::Queue, TEST_QUEUE_BUFSIZE, "queue");
shm.addChannel(ShmChannel::Request, TEST_REQUEST_BUFSIZE, "request");
shm.addChannel(ShmChannel::Request, 0, "sync");
shm.create();
LOG_INFO("server: created shared memory interface " << shm.path());
// spawn child process
#ifdef _WIN32
STARTUPINFOA si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
char cmdLine[256];
snprintf(cmdLine, sizeof(cmdLine), "%s %s", APPNAME, shm.path().c_str());
if (!CreateProcessA(NULL, cmdLine, NULL, NULL,
TRUE, 0, NULL, NULL, &si, &pi)){
throw Error(Error::SystemError, "CreateProcess() failed!");
}
#else
// hack to get absolute app path
Dl_info dlinfo;
if (!dladdr((void *)server_run, &dlinfo)){
throw Error(Error::SystemError, "dladdr() failed");
}
// fork
pid_t pid = fork();
if (pid == -1) {
throw Error(Error::SystemError, "fork() failed!");
} else if (pid == 0) {
// child process
if (execl(dlinfo.dli_fname, APPNAME, shm.path().c_str(), nullptr) < 0){
throw Error(Error::SystemError, "execl() failed!");
}
}
// continue with parent process
#endif
auto& sync = shm.getChannel(2);
sync.post();
sync.waitReply();
#if TEST_QUEUE
server_test_queue(shm);
sync.post();
sync.waitReply();
#endif
#if TEST_REQUEST
server_test_request(shm);
sync.post();
sync.waitReply();
#endif
#if TEST_BENCHMARK
server_benchmark(shm);
sync.post();
sync.waitReply();
#endif
LOG_DEBUG("server: waiting for client");
#ifdef _WIN32
DWORD code = -1;
if (WaitForSingleObject(pi.hProcess, INFINITE) != 0){
throw Error(Error::SystemError, "WaitForSingleObject() failed!");
}
if (!GetExitCodeProcess(pi.hProcess, &code)){
throw Error(Error::SystemError, "GetExitCodeProcess() failed!");
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
#else
int code = -1;
int status = 0;
if (waitpid(pid, &status, 0) == pid) {
if (WIFEXITED(status)) {
code = WEXITSTATUS(status);
} else if (WIFSIGNALED(status)) {
int sig = WTERMSIG(status);
std::stringstream msg;
msg << "child process terminated with signal "
<< sig << " (" << strsignal(sig) << ")";
throw Error(Error::SystemError, msg.str());
} else {
throw Error(Error::SystemError, "child process terminated with status "
+ std::to_string(status));
}
} else {
throw Error(Error::SystemError, "waitpid() failed: " + errorMessage(errno));
}
#endif
LOG_INFO("child process finished with exit code " << code);
return EXIT_SUCCESS;
}
int client_run(const char *path){
LOG_INFO("---");
LOG_INFO("client: start");
LOG_INFO("---");
ShmInterface shm;
shm.connect(path);
LOG_INFO("client: connected to shared memory interface " << path);
auto& sync = shm.getChannel(2);
sync.wait();
sync.postReply();
#if TEST_QUEUE
client_test_queue(shm);
sync.wait();
sync.postReply();
#endif
#if TEST_REQUEST
client_test_request(shm);
sync.wait();
sync.postReply();
#endif
#if TEST_BENCHMARK
client_benchmark(shm);
sync.wait();
sync.postReply();
#endif
return EXIT_SUCCESS;
}
int main(int argc, const char *argv[]){
#if TEST_REALTIME
setThreadPriority(Priority::High);
#endif
try {
if (argc > 1) {
return client_run(argv[1]);
} else {
return server_run();
}
} catch (const std::exception& e){
LOG_ERROR(e.what());
return EXIT_FAILURE;
}
}
|