Line data Source code
1 : // Copyright (c) 2012-2024 Made to Order Software Corp. All Rights Reserved 2 : // 3 : // https://snapwebsites.org/project/eventdispatcher 4 : // contact@m2osw.com 5 : // 6 : // This program is free software; you can redistribute it and/or modify 7 : // it under the terms of the GNU General Public License as published by 8 : // the Free Software Foundation; either version 2 of the License, or 9 : // (at your option) any later version. 10 : // 11 : // This program is distributed in the hope that it will be useful, 12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 : // GNU General Public License for more details. 15 : // 16 : // You should have received a copy of the GNU General Public License along 17 : // with this program; if not, write to the Free Software Foundation, Inc., 18 : // 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 : 20 : // self 21 : // 22 : #include "catch_main.h" 23 : 24 : 25 : // cppprocess 26 : // 27 : #include <cppprocess/process_info.h> 28 : 29 : 30 : // C 31 : // 32 : #include <sys/resource.h> 33 : #include <sys/times.h> 34 : 35 : 36 : // last include 37 : // 38 : #include <snapdev/poison.h> 39 : 40 : 41 : 42 1 : CATCH_TEST_CASE("Process Info", "[process]") 43 : { 44 1 : CATCH_START_SECTION("check ourselves") 45 : { 46 : // information about ourselves 47 : // 48 1 : cppprocess::process_info info(getpid()); 49 : 50 1 : CATCH_REQUIRE(info.get_pid() == getpid()); 51 1 : CATCH_REQUIRE(info.get_ppid() == getppid()); 52 1 : CATCH_REQUIRE(info.get_pgid() == getpgid(getpid())); 53 : 54 1 : rusage usage; 55 1 : getrusage(RUSAGE_SELF, &usage); 56 : 57 1 : rusage cusage; 58 1 : getrusage(RUSAGE_CHILDREN, &cusage); 59 : 60 1 : tms process_times = {}; 61 1 : times(&process_times); 62 : 63 : { 64 1 : char ** args = SNAP_CATCH2_NAMESPACE::g_argv; 65 1 : CATCH_REQUIRE(info.get_name() == "unittest"); 66 1 : CATCH_REQUIRE(args[0] == info.get_command()); 67 1 : CATCH_REQUIRE(info.get_basename() == "unittest"); 68 : 69 1 : int idx = 1; 70 14 : for(; args[idx] != nullptr; ++idx) 71 : { 72 13 : CATCH_REQUIRE(info.get_arg(idx) == args[idx]); 73 : } 74 1 : CATCH_REQUIRE(static_cast<std::size_t>(idx) == info.get_args_size()); 75 : } 76 : 77 1 : CATCH_REQUIRE(info.get_state() == cppprocess::process_state_t::PROCESS_STATE_RUNNING); 78 : 79 : { 80 1 : unsigned long long utime(0); 81 1 : unsigned long long stime(0); 82 1 : unsigned long long cutime(0); 83 1 : unsigned long long cstime(0); 84 1 : info.get_times(utime, stime, cutime, cstime); 85 : 86 : //std::cerr << "times -- " << utime 87 : //<< ", " << stime 88 : //<< ", " << cutime 89 : //<< ", " << cstime 90 : //<< "\n"; 91 : //std::cerr << "direct times -- " << process_times.tms_utime 92 : //<< ", " << process_times.tms_stime 93 : //<< ", " << process_times.tms_cutime 94 : //<< ", " << process_times.tms_cstime 95 : //<< "\n"; 96 : 97 1 : std::int64_t const ut(utime - process_times.tms_utime); 98 1 : CATCH_REQUIRE(labs(ut) <= 2); 99 : 100 1 : std::int64_t const st(stime - process_times.tms_stime); 101 1 : CATCH_REQUIRE(labs(st) <= 2); 102 : 103 1 : std::int64_t const cut(cutime - process_times.tms_cutime); 104 1 : CATCH_REQUIRE(labs(cut) <= 2); 105 : 106 1 : std::int64_t const cst(cstime - process_times.tms_cstime); 107 1 : CATCH_REQUIRE(labs(cst) <= 2); 108 : } 109 : 110 : // info says 20, getpriority() says 0 111 : //CATCH_REQUIRE(info.get_priority() == getpriority(PRIO_PROCESS, 0)); 112 1 : CATCH_REQUIRE(info.get_nice() == nice(0)); 113 : 114 : { 115 1 : std::uint64_t pf_major(0); 116 1 : std::uint64_t pf_minor(0); 117 1 : info.get_page_faults(pf_major, pf_minor); 118 : 119 : // WARNING 120 : // the following are rather random... as we add more tests 121 : // this can increase 122 : // 123 1 : std::int64_t const maj(pf_major - usage.ru_majflt); 124 1 : CATCH_REQUIRE(labs(maj) < 100); 125 : 126 1 : std::int64_t const min(pf_minor - usage.ru_minflt); 127 1 : CATCH_REQUIRE(labs(min) < 100); 128 : } 129 : 130 : // TODO: see how to use the rusage data to more or less match these 131 1 : CATCH_REQUIRE(info.get_total_size() != 0); 132 1 : CATCH_REQUIRE(info.get_rss_size() != 0); 133 : 134 : { 135 1 : int maj(0); 136 1 : int min(0); 137 1 : info.get_tty(maj, min); 138 : // how do we compare these maj:min with our tty? 139 : } 140 1 : } 141 1 : CATCH_END_SECTION() 142 1 : } 143 : 144 : 145 : // vim: ts=4 sw=4 et