339 lines
11 KiB
C
339 lines
11 KiB
C
#include "femlite_cclite_ipc.h"
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#ifdef _WIN32
|
|
#include <windows.h>
|
|
#endif
|
|
|
|
static void fail(int code, int ierr)
|
|
{
|
|
fprintf(stderr, "test_cclite_worker_process failed at %d: ierr=%d\n",
|
|
code, ierr);
|
|
exit(1);
|
|
}
|
|
|
|
#ifdef _WIN32
|
|
static void quote_arg(char *out, size_t capacity, const char *value)
|
|
{
|
|
size_t len = strlen(value);
|
|
if (len + 3 > capacity) {
|
|
fail(10, 0);
|
|
}
|
|
out[0] = '"';
|
|
memcpy(out + 1, value, len);
|
|
out[len + 1] = '"';
|
|
out[len + 2] = '\0';
|
|
}
|
|
|
|
static HANDLE create_mapping(const char *name, size_t size, void **view)
|
|
{
|
|
HANDLE map = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL,
|
|
PAGE_READWRITE, 0, (DWORD)size, name);
|
|
if (map == NULL) {
|
|
fail(20, (int)GetLastError());
|
|
}
|
|
*view = MapViewOfFile(map, FILE_MAP_ALL_ACCESS, 0, 0, size);
|
|
if (*view == NULL) {
|
|
fail(21, (int)GetLastError());
|
|
}
|
|
memset(*view, 0, size);
|
|
return map;
|
|
}
|
|
|
|
static void init_data_block(void *view, int record_type, int capacity,
|
|
int stride)
|
|
{
|
|
femlite_cclite_data_header *header =
|
|
(femlite_cclite_data_header *)view;
|
|
femlite_cclite_init_data_header(header, record_type, capacity, stride);
|
|
}
|
|
|
|
static PROCESS_INFORMATION start_worker(const char *femlite_exe,
|
|
const char *prefix,
|
|
const char *case_file,
|
|
const char *log_file,
|
|
const char *output_dir,
|
|
int timeout_ms)
|
|
{
|
|
PROCESS_INFORMATION pi;
|
|
STARTUPINFOA si;
|
|
char quoted_exe[1024];
|
|
char quoted_case[1024];
|
|
char quoted_log[1024];
|
|
char quoted_output[1024];
|
|
char command[4096];
|
|
|
|
memset(&pi, 0, sizeof(pi));
|
|
memset(&si, 0, sizeof(si));
|
|
si.cb = sizeof(si);
|
|
|
|
quote_arg(quoted_exe, sizeof(quoted_exe), femlite_exe);
|
|
quote_arg(quoted_case, sizeof(quoted_case), case_file);
|
|
quote_arg(quoted_log, sizeof(quoted_log), log_file);
|
|
quote_arg(quoted_output, sizeof(quoted_output), output_dir);
|
|
snprintf(command, sizeof(command),
|
|
"%s --cclite-worker --worker-backend ipc --case %s "
|
|
"--ipc-name %s --data-prefix %s --log %s --output-dir %s "
|
|
"--timeout-ms %d",
|
|
quoted_exe, quoted_case, prefix, prefix, quoted_log,
|
|
quoted_output, timeout_ms);
|
|
|
|
if (!CreateProcessA(NULL, command, NULL, NULL, FALSE,
|
|
CREATE_NO_WINDOW, NULL, NULL, &si, &pi)) {
|
|
fail(30, (int)GetLastError());
|
|
}
|
|
return pi;
|
|
}
|
|
|
|
static void send_command(void *command_view, HANDLE command_event,
|
|
HANDLE done_event, int seq, int command_id,
|
|
int step_id)
|
|
{
|
|
femlite_cclite_command_block *block =
|
|
(femlite_cclite_command_block *)command_view;
|
|
|
|
if (!ResetEvent(command_event)) {
|
|
fail(40 + seq, (int)GetLastError());
|
|
}
|
|
if (!ResetEvent(done_event)) {
|
|
fail(80 + seq, (int)GetLastError());
|
|
}
|
|
femlite_cclite_init_command_block(block);
|
|
block->seq = seq;
|
|
block->command_id = command_id;
|
|
block->step_id = step_id;
|
|
if (!SetEvent(command_event)) {
|
|
fail(120 + seq, (int)GetLastError());
|
|
}
|
|
}
|
|
|
|
static void wait_done(void *command_view, HANDLE done_event, int seq,
|
|
int expected_state)
|
|
{
|
|
femlite_cclite_command_block *block =
|
|
(femlite_cclite_command_block *)command_view;
|
|
DWORD rc = WaitForSingleObject(done_event, 10000);
|
|
if (rc != WAIT_OBJECT_0) {
|
|
fail(200 + seq, (int)rc);
|
|
}
|
|
if (block->ack_seq != seq) {
|
|
fail(300 + seq, block->ack_seq);
|
|
}
|
|
if (expected_state != 0 && block->worker_state != expected_state) {
|
|
fail(400 + seq, block->worker_state);
|
|
}
|
|
if (expected_state != FEMLITE_CCL_STATE_ERROR && block->error_code != 0) {
|
|
fail(500 + seq, block->error_code);
|
|
}
|
|
}
|
|
|
|
static int run_command(void *command_view, HANDLE command_event,
|
|
HANDLE done_event, int seq, int command_id,
|
|
int step_id, int expected_state)
|
|
{
|
|
send_command(command_view, command_event, done_event, seq, command_id,
|
|
step_id);
|
|
wait_done(command_view, done_event, seq, expected_state);
|
|
return seq + 1;
|
|
}
|
|
|
|
static void run_process_loop(const char *femlite_exe, const char *source_dir,
|
|
int steps, int timeout_mode, int bad_case_mode)
|
|
{
|
|
char prefix[128];
|
|
char command_name[192];
|
|
char command_event_name[192];
|
|
char done_event_name[192];
|
|
char facet_name[192];
|
|
char load_name[192];
|
|
char case_file[1024];
|
|
char log_file[1024];
|
|
char output_dir[1024];
|
|
HANDLE command_map;
|
|
HANDLE facet_map;
|
|
HANDLE load_map;
|
|
HANDLE command_event;
|
|
HANDLE done_event;
|
|
void *command_view;
|
|
void *facet_view;
|
|
void *load_view;
|
|
PROCESS_INFORMATION pi;
|
|
DWORD rc;
|
|
DWORD exit_code;
|
|
int seq = 1;
|
|
int step;
|
|
size_t facet_size;
|
|
size_t load_size;
|
|
femlite_cclite_data_header *facet_header;
|
|
|
|
snprintf(prefix, sizeof(prefix), "Local\\FemLiteWorkerProc_%lu_%d",
|
|
(unsigned long)GetCurrentProcessId(), steps);
|
|
if (femlite_cclite_format_name(prefix, "COMMAND_BLOCK", command_name,
|
|
sizeof(command_name)) != 0) {
|
|
fail(1000, 0);
|
|
}
|
|
if (femlite_cclite_format_name(prefix, "COMMAND_EVENT",
|
|
command_event_name,
|
|
sizeof(command_event_name)) != 0) {
|
|
fail(1001, 0);
|
|
}
|
|
if (femlite_cclite_format_name(prefix, "DONE_EVENT", done_event_name,
|
|
sizeof(done_event_name)) != 0) {
|
|
fail(1002, 0);
|
|
}
|
|
if (femlite_cclite_format_name(prefix, "FEM_TO_CCL_FACET", facet_name,
|
|
sizeof(facet_name)) != 0) {
|
|
fail(1003, 0);
|
|
}
|
|
if (femlite_cclite_format_name(prefix, "CCL_TO_FEM_LOAD", load_name,
|
|
sizeof(load_name)) != 0) {
|
|
fail(1004, 0);
|
|
}
|
|
|
|
command_map = create_mapping(command_name,
|
|
sizeof(femlite_cclite_command_block),
|
|
&command_view);
|
|
facet_size = sizeof(femlite_cclite_data_header) +
|
|
64 * sizeof(femlite_cclite_facet_record_aor_v1);
|
|
load_size = sizeof(femlite_cclite_data_header) +
|
|
8 * sizeof(femlite_cclite_load_record_aor_v1);
|
|
facet_map = create_mapping(facet_name, facet_size, &facet_view);
|
|
load_map = create_mapping(load_name, load_size, &load_view);
|
|
init_data_block(facet_view, FEMLITE_CCL_DATA_RECORD_FEM_TO_CCL_FACET,
|
|
64, (int)sizeof(femlite_cclite_facet_record_aor_v1));
|
|
init_data_block(load_view, FEMLITE_CCL_DATA_RECORD_CCL_TO_FEM_LOAD,
|
|
8, (int)sizeof(femlite_cclite_load_record_aor_v1));
|
|
|
|
command_event = CreateEventA(NULL, FALSE, FALSE, command_event_name);
|
|
done_event = CreateEventA(NULL, TRUE, FALSE, done_event_name);
|
|
if (command_event == NULL || done_event == NULL) {
|
|
fail(1005, (int)GetLastError());
|
|
}
|
|
|
|
if (bad_case_mode) {
|
|
snprintf(case_file, sizeof(case_file), "%s\\tests\\data\\missing.inp",
|
|
source_dir);
|
|
} else {
|
|
snprintf(case_file, sizeof(case_file),
|
|
"%s\\examples\\c3d4_one_element.inp", source_dir);
|
|
}
|
|
snprintf(log_file, sizeof(log_file), "%s\\build-codex-gcc\\worker_proc_%d.log",
|
|
source_dir, steps);
|
|
snprintf(output_dir, sizeof(output_dir),
|
|
"%s\\build-codex-gcc\\worker_proc_results_%d",
|
|
source_dir, steps);
|
|
pi = start_worker(femlite_exe, prefix, case_file, log_file,
|
|
output_dir, timeout_mode ? 100 : 10000);
|
|
|
|
wait_done(command_view, done_event, 0, FEMLITE_CCL_STATE_READY);
|
|
if (timeout_mode) {
|
|
rc = WaitForSingleObject(pi.hProcess, 10000);
|
|
if (rc != WAIT_OBJECT_0) {
|
|
fail(1100, (int)rc);
|
|
}
|
|
if (!GetExitCodeProcess(pi.hProcess, &exit_code)) {
|
|
fail(1101, (int)GetLastError());
|
|
}
|
|
if (exit_code == 0) {
|
|
fail(1102, (int)exit_code);
|
|
}
|
|
goto cleanup;
|
|
}
|
|
if (bad_case_mode) {
|
|
seq = run_command(command_view, command_event, done_event, seq,
|
|
FEMLITE_CCL_CMD_INIT, 0, FEMLITE_CCL_STATE_READY);
|
|
seq = run_command(command_view, command_event, done_event, seq,
|
|
FEMLITE_CCL_CMD_READ_INPUT, 0,
|
|
FEMLITE_CCL_STATE_ERROR);
|
|
rc = WaitForSingleObject(pi.hProcess, 10000);
|
|
if (rc != WAIT_OBJECT_0) {
|
|
fail(1150, (int)rc);
|
|
}
|
|
if (!GetExitCodeProcess(pi.hProcess, &exit_code)) {
|
|
fail(1151, (int)GetLastError());
|
|
}
|
|
if (exit_code == 0) {
|
|
fail(1152, (int)exit_code);
|
|
}
|
|
goto cleanup;
|
|
}
|
|
|
|
seq = run_command(command_view, command_event, done_event, seq,
|
|
FEMLITE_CCL_CMD_INIT, 0, FEMLITE_CCL_STATE_READY);
|
|
seq = run_command(command_view, command_event, done_event, seq,
|
|
FEMLITE_CCL_CMD_READ_INPUT, 0, FEMLITE_CCL_STATE_READY);
|
|
seq = run_command(command_view, command_event, done_event, seq,
|
|
FEMLITE_CCL_CMD_BUILD_MODEL, 0, FEMLITE_CCL_STATE_READY);
|
|
seq = run_command(command_view, command_event, done_event, seq,
|
|
FEMLITE_CCL_CMD_EXPORT_IFACE, 0,
|
|
FEMLITE_CCL_STATE_READY);
|
|
|
|
facet_header = (femlite_cclite_data_header *)facet_view;
|
|
if (facet_header->record_count <= 0) {
|
|
fail(1200, facet_header->record_count);
|
|
}
|
|
|
|
for (step = 1; step <= steps; ++step) {
|
|
((femlite_cclite_data_header *)load_view)->record_count = 0;
|
|
seq = run_command(command_view, command_event, done_event, seq,
|
|
FEMLITE_CCL_CMD_IMPORT_LOADS, step,
|
|
FEMLITE_CCL_STATE_READY);
|
|
seq = run_command(command_view, command_event, done_event, seq,
|
|
FEMLITE_CCL_CMD_ADVANCE, step,
|
|
FEMLITE_CCL_STATE_READY);
|
|
}
|
|
|
|
seq = run_command(command_view, command_event, done_event, seq,
|
|
FEMLITE_CCL_CMD_WRITE_OUTPUT, steps,
|
|
FEMLITE_CCL_STATE_READY);
|
|
seq = run_command(command_view, command_event, done_event, seq,
|
|
FEMLITE_CCL_CMD_STOP, steps, FEMLITE_CCL_STATE_DONE);
|
|
rc = WaitForSingleObject(pi.hProcess, 10000);
|
|
if (rc != WAIT_OBJECT_0) {
|
|
fail(1300, (int)rc);
|
|
}
|
|
if (!GetExitCodeProcess(pi.hProcess, &exit_code) || exit_code != 0) {
|
|
fail(1301, (int)exit_code);
|
|
}
|
|
|
|
cleanup:
|
|
CloseHandle(pi.hThread);
|
|
CloseHandle(pi.hProcess);
|
|
CloseHandle(done_event);
|
|
CloseHandle(command_event);
|
|
UnmapViewOfFile(load_view);
|
|
UnmapViewOfFile(facet_view);
|
|
UnmapViewOfFile(command_view);
|
|
CloseHandle(load_map);
|
|
CloseHandle(facet_map);
|
|
CloseHandle(command_map);
|
|
}
|
|
#endif
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
#ifdef _WIN32
|
|
int steps;
|
|
int timeout_mode = 0;
|
|
int bad_case_mode = 0;
|
|
if (argc < 4) {
|
|
fail(1, argc);
|
|
}
|
|
steps = atoi(argv[3]);
|
|
if (argc > 4 && strcmp(argv[4], "timeout") == 0) {
|
|
timeout_mode = 1;
|
|
}
|
|
if (argc > 4 && strcmp(argv[4], "badcase") == 0) {
|
|
bad_case_mode = 1;
|
|
}
|
|
run_process_loop(argv[1], argv[2], steps, timeout_mode, bad_case_mode);
|
|
#else
|
|
(void)argc;
|
|
(void)argv;
|
|
#endif
|
|
return 0;
|
|
}
|