Skip to content

SiddiqSoft/WinProcessInfo

Repository files navigation

WinProcessInfo : Sample Windows process information

CodeQL Build Status

Objective

Provide for a simple "stats" for a daemon/service without the heft of the full process information library.

  • Minimal footprint
  • Serializer for nlohmann::json (you must include the library prior to this header file)
  • Serializer for std::format (if supported by your compiler)

Requirements

  • We use nlohmann::json only in our tests and the library is aware to provide a conversion operator if library is detected.
  • We use std::format if present

Usage

Interface

WinProcessInfo
- uptime()
- snapshot()
- serializer for nlohmann::json
- serializer for std::format

Example

#include "gtest/gtest.h"
#include <format>
#include "nlohmann/json.hpp"
#include "siddiqsoft/WinProcessInfo.hpp"

TEST(examples, Example2)
{
   try {
      siddiqsoft::WinProcessInfo procInfo;

      // We must perform the snapshot to obtain memory and thread usage
      procInfo.snapshot();

      // tip: do not use brace-init/assignment as it will create an array instead of object!
      nlohmann::json info(procInfo);

      std::cerr << info.dump() << std::endl;
      EXPECT_EQ(GetCurrentProcessId(), info.value("processId", 0));
   }
   catch (std::exception& e) {
      EXPECT_TRUE(false) << e.what(); // if we throw then the test fails.
   }
}

Output

Member Field json Field Comments
cpuCores cpuCount Number of cores/cpu
cpuHandles cpuHandles Handle count
cpuThreads cpuThreads Thread count. This is expensive.
memPeakWorkingSet memPeakWorkingSet Peak virtual working memory in Kbytes.
memWorkingSet memWorkingSet Current working set in Kbytes.
memPrivate memPrivate Current physically allocated memory for this process in Kbytes.
nameHostname hostname The local hostname
nameDomainName domain The fully qualified domain portion
nameHostnamePhysical localFqdn The local physical hostname
nameFqdn fqdn The fully qualified dns name
processId processId The process id
timeStartup timeStartup The startup timestamp as ISO 8601 format. This is the time of instance of this object.
  timeCurrent The current timestamp as ISO 8601 format when the serialization took place.
  uptime The number of microseconds between the timeStartup and timeCurrent.
Use the method uptime() and duration_cast<> to your desired ratio.
{
    "cpuCores": 8,
    "cpuHandles": 48,
    "cpuThreads": 4,
    "domain": "",
    "fqdn": "istanbul",
    "hostname": "istanbul",
    "localFqdn": "istanbul",
    "memPeakWorkingSet": 42960, // KBytes
    "memPrivateBytes": 84452,   // Kbytes
    "memWorkingSet": 42584,     // Kbytes
    "processId": 10828,
    "timeCurrent": "2021-07-30T06:43:54.8649027Z",
    "timeStartup": "2021-07-30T06:43:54.8599558Z",
    "uptime": 5124              // Microseconds
}

© 2021 Siddiq Software LLC. All rights reserved.