/* * ===================================================================================== * * Filename: status_main.cc * * Description: * * Version: 1.0 * Created: 2021年04月16日 12时17分07秒 * Revision: none * Compiler: gcc * * Author: Li Chao (), lichao@aiotlink.com * Organization: * * ===================================================================================== */ #include "app_arg.h" #include "box.h" #include "defs.h" #include "shm.h" #include "signalhandle.h" #include #include #include using namespace std::chrono_literals; using namespace std::chrono; using namespace bhome_shm; int status_main(int argc, char const *argv[]) { AppArg args(argc, argv); auto shm_name = args.Get("shm", BHomeShm().name()); auto shm_size = std::atol(args.Get("size", "").c_str()); if (shm_size <= 0 || shm_size > 512) { shm_size = 50; } auto DisplayName = [&]() -> std::string { return shm_name; }; SharedMemory shm(shm_name, 1024 * 1024 * shm_size); shm_size = shm.get_size() / 1024 / 1024; printf("monitoring shm : %s, size : %ldM\n", DisplayName().c_str(), shm_size); std::atomic run(true); auto Now = []() { return steady_clock::now(); }; auto ToMs = [](auto tp) { return duration_cast(tp.time_since_epoch()).count(); }; auto showStatus = [&]() { auto next = Now(); const uint64_t start = ToMs(next); auto last = 0ul; while (run) { std::this_thread::sleep_until(next); auto passed = ToMs(next) - start; auto sec = passed / 1000; auto cur = shm.get_free_memory(); auto Pretty = [](int64_t nbyte) { char sign = '+'; if (nbyte < 0) { nbyte = -nbyte; sign = '-'; } const int Kb = 1024; const int Mb = Kb * 1024; int nmb = nbyte / Mb; auto left = nbyte - nmb * Mb; int nkb = left / Kb; int nb = left - nkb * Kb; char buf[64] = {0}; int n = sprintf(buf, " %4dM%4dK%4dB", nmb, nkb, nb); int start = (nmb > 0) ? 0 : ((nkb > 0) ? 5 : 10); buf[start] = sign; return std::string(buf + start); }; char buf[200] = "\n"; auto Print = [&](bool new_line) { int n = sprintf(buf, "\r%6lds avail : %12ld = %s %6lds", sec, cur, Pretty(cur).c_str() + 1, sec); printf("%s", buf); if (new_line) { auto diff = cur - last; printf(" (%+6ld = %s)\n", diff, Pretty(diff).c_str()); printf("%s", buf); } fflush(stdout); }; Print(cur != last); last = cur; next += 1000ms; } }; std::thread t(showStatus); WaitForSignals({SIGINT, SIGTERM}); run.store(false); t.join(); return 0; } namespace { static bool install = BoxInstall("bhshmq_status", status_main, "bhome status program."); }