lichao
2021-06-23 c1e39e20ca42b21eeac8b5068fa1f921bf9a070f
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
/*
 * =====================================================================================
 *
 *       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 <atomic>
#include <chrono>
#include <thread>
 
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<bool> run(true);
 
    auto Now = []() { return steady_clock::now(); };
    auto ToMs = [](auto tp) { return duration_cast<milliseconds>(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.");
}