void *load_check(void *param) { //int n=(int)param; cpu_t cpu1, cpu2; while(1) { cpu_t *p_cpu1 = cpu_check(&cpu1); sleep(1); cpu_t *p_cpu2 = cpu_check(&cpu2); print_cpu(p_cpu1, p_cpu2); } pthread_exit(0); }
void check_mainloop(void) { time_t now = 0, then = 0; useconds_t sleeptime; int delta = 0; /* init */ status_alerted.alert_load = false; status_alerted.alert_disk = false; status_alerted.alert_cpu = false; sleeptime = 100000; numcpus = num_cpus(); http_fetch_url(hburl); vbprintf("Found %d cpus\n", numcpus); database_init(); /* * XXX: Right here we're just spinning in place. The reason for this * is to be able to have different intervals for the checking process * (disk/cpu/load), and also for the heartbeat-process, which might * check at different intervals. I might separate this out into * another file so we have a rudimentary timer-based scheduler that * can shoot off different functions at variable intervals. */ time(&then); while (1) { int sampletrig = 0, hbtrig = 0; time(&now); delta = (int) now - (int) then; sampletrig = delta % interval; hbtrig = delta % hbinterval; if (!sampletrig) { load_check(); disk_check(diskpaths); cpu_check(); check_alert(); sleep(1); /* make sure trig status is over */ } if (!hbtrig) { http_fetch_url(hburl); sleep(1); /* make sure trig status is over */ } usleep(sleeptime); } }
int calculator(FILE* strin, FILE* strout, cpu* my_cpu) { VERIFY(my_cpu != NULL); const char MAXLINE = 10; char word[MAXLINE] = {}; const char PUSH_MAXLINE = 50; char push_word[PUSH_MAXLINE] = {}; int c = 0; double temp = 0; int cond = 0; fprintf(stdout, "# This is prototype of processor unit which calculates expressions\n" "# written in inverted Polish way (or something like that)\n" "# There are commands available:\n" "\n push {number}\n" " pop\n" " add\n" " sub\n" " mul\n" " div\n" " sin\n" " cos\n" " tan\n" " sqrt\n" " pow\n" " end\n" "PLEASE, USE [.] TO DIVIDE FLOAT PART\n" "Here you go\n"); while (true) { cond = cpu_check(my_cpu); VERIFY(cond == CPU_CHECK_OK); fscanf(strin,"%s", &word); c = CHECK_COMMAND(word, PUSH) CHECK_COMMAND(word, POP) CHECK_COMMAND(word, ADD) CHECK_COMMAND(word, SUB) CHECK_COMMAND(word, MUL) CHECK_COMMAND(word, DIV) CHECK_COMMAND(word, SIN) CHECK_COMMAND(word, COS) CHECK_COMMAND(word, TAN) CHECK_COMMAND(word, SQRT) CHECK_COMMAND(word, POW) CHECK_COMMAND(word, END) CHECK_COMMAND(word, DUMP) CMD_NONE; switch (c) { case CMD_PUSH: cond = fscanf(strin, "%s", push_word); VERIFY(cond); cond = cpu_catch_error(strout, my_cpu, cpu_push(my_cpu, push_word)); //if (cond == CPU_ERROR_CATCHER_BAD) VERIFY(!"ERROR HAS BEEN CAUGHT"); if (cond == CPU_ERROR_PUSH_BAD_TOKEN) { fprintf(strout, "\nPUSH BAD TOKEN [%s]\n", push_word); break; } break; case CMD_POP: cond = cpu_catch_error(strout, my_cpu, cpu_pop(strout, my_cpu, &temp)); //if (cond == CPU_ERROR_CATCHER_BAD) VERIFY(!"ERROR HAS BEEN CAUGHT"); break; case CMD_ADD: cond = cpu_catch_error(strout, my_cpu, cpu_add(my_cpu)); //if (cond == CPU_ERROR_CATCHER_BAD) VERIFY(!"ERROR HAS BEEN CAUGHT"); break; case CMD_SUB: cond = cpu_catch_error(strout, my_cpu, cpu_sub(my_cpu)); //if (cond == CPU_ERROR_CATCHER_BAD) VERIFY(!"ERROR HAS BEEN CAUGHT"); break; case CMD_MUL: cond = cpu_catch_error(strout, my_cpu, cpu_mul(my_cpu)); //if (cond == CPU_ERROR_CATCHER_BAD) VERIFY(!"ERROR HAS BEEN CAUGHT"); break; case CMD_DIV: cond = cpu_catch_error(strout, my_cpu, cpu_div(my_cpu)); //if (cond == CPU_ERROR_CATCHER_BAD) VERIFY(!"ERROR HAS BEEN CAUGHT"); break; case CMD_SIN: cond = cpu_catch_error(strout, my_cpu, cpu_sin(my_cpu)); //if (cond == CPU_ERROR_CATCHER_BAD) VERIFY(!"ERROR HAS BEEN CAUGHT"); break; case CMD_COS: cond = cpu_catch_error(strout, my_cpu, cpu_cos(my_cpu)); //if (cond == CPU_ERROR_CATCHER_BAD) VERIFY(!"ERROR HAS BEEN CAUGHT"); break; case CMD_TAN: cond = cpu_catch_error(strout, my_cpu, cpu_tan(my_cpu)); //if (cond == CPU_ERROR_CATCHER_BAD) VERIFY(!"ERROR HAS BEEN CAUGHT"); break; case CMD_SQRT: cond = cpu_catch_error(strout, my_cpu, cpu_sqrt(my_cpu)); //if (cond == CPU_ERROR_CATCHER_BAD) VERIFY(!"ERROR HAS BEEN CAUGHT"); break; case CMD_POW: cond = cpu_catch_error(strout, my_cpu, cpu_pow(my_cpu)); //if (cond == CPU_ERROR_CATCHER_BAD) VERIFY(!"ERROR HAS BEEN CAUGHT"); break; case CMD_DUMP: cond = cpu_catch_error(strout, my_cpu, cpu_dump(strout, my_cpu)); //if (cond == CPU_ERROR_CATCHER_BAD) VERIFY(!"ERROR HAS BEEN CAUGHT"); break; case CMD_END: return 0; default: fprintf(strout, "WRONG TOKEN [%s]\n", word); break; } cond = cpu_check(my_cpu); VERIFY(cond == CPU_CHECK_OK); } }
int main() { try { std::vector<cl::Device> devices; // select platform cl::Platform platform = selectPlatform(); // select device platform.getDevices(CL_DEVICE_TYPE_ALL, &devices); cl::Device device = selectDevice(devices); // create context context = cl::Context(devices); // create command queue queue = cl::CommandQueue(context, device, CL_QUEUE_PROFILING_ENABLE); // load opencl source std::ifstream cl_file("inclusive_scan.cl"); std::string cl_string{std::istreambuf_iterator<char>(cl_file), std::istreambuf_iterator<char>()}; cl::Program::Sources source(1, std::make_pair(cl_string.c_str(), cl_string.length() + 1)); // create programm program = cl::Program(context, source); // compile opencl source try { program.build(devices); size_t input_size; std::ifstream input_file("input.txt"); input_file >> input_size; std::vector<float> input(input_size); // for (size_t i = 0; i < input_size; ++i) { // input[i] = i % 10; // } for (int i = 0; i < input_size; i++) { input_file >> input[i]; } std::vector<float> output(input_size, 0); cl::Buffer dev_input (context, CL_MEM_READ_ONLY, sizeof(float) * input_size); queue.enqueueWriteBuffer(dev_input, CL_TRUE, 0, sizeof(float) * input_size, &input[0]); cl::Buffer dev_output = inclusive_scan(dev_input, input_size); queue.enqueueReadBuffer(dev_output, CL_TRUE, 0, sizeof(float) * input_size, &output[0]); queue.finish(); cpu_check(input, output); std::ofstream output_file("output.txt"); for (int i = 0; i < input_size; i++) { output_file << output[i] << " "; } } catch (cl::Error const & e) { std::string log_str = program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(device); std::cout << std::endl << e.what() << " : " << e.err() << std::endl; std::cout << log_str; return 0; } } catch (cl::Error const & e) { std::cout << "Error: " << e.what() << " #" << e.err() << std::endl; } return 0; }