Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: cpu sample debug #1852

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 147 additions & 0 deletions cmd/debug/cpu_load_windows.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#include <iostream>
#define _WIN32_WINNT 0x0602
#include <cstdint>
#include <ctime>
#include <iterator>
#include <locale>
#include <stdlib.h>
#include <windows.h>
// #include <processthreadsapi.h>

class Processor {};

uint64_t FromFileTime(const FILETIME &ft) {
// ULARGE_INTEGER uli = {0};
// uli.LowPart = ft.dwLowDateTime;
// uli.HighPart = ft.dwHighDateTime;
// return uli.QuadPart;
return (((uint64_t)ft.dwHighDateTime) << 32) + (uint64_t)ft.dwLowDateTime;
// float LOT = 0.0000001f;
// float HIT = LOT * 4294967296.0f;
// return HIT * (float)ft.dwHighDateTime + LOT * (float)ft.dwLowDateTime;
}

class Usage : public Processor {
public:
// int now()
// {
// FILETIME a0, a1, a2, b0, b1, b2;

// GetSystemTimes(&a0, &a1, &a2);
// SleepEx(250, false);
// GetSystemTimes(&b0, &b1, &b2);

// // attempt to get the actual value instead of the pointer and convert
// it to float/double/int float idle0 = a0; float idle1 = b0; float
// kernel0 = a1; float kernel1 = b1; float user0 = a2; float user1 = b2;

// float idl = idle1 - idle0;
// float ker = kernel0 - kernel1;
// float usr = user0 - user1;

// float cpu = (ker - idl + usr) * 100 / (ker + usr);

// return cpu;
// }

FILETIME a0, a1, a2;

void utcExample() {
// Example of the very popular RFC 3339 format UTC time
std::time_t time = std::time({});
char timeString[std::size("yyyy-mm-dd hh:mm:ss")];
std::strftime(std::data(timeString), std::size(timeString), "%F %T",
std::gmtime(&time));
std::string str(timeString);
printf("| %s ", str);
// return str;
}

void now() {
FILETIME b0, b1, b2;

// GetSystemTimes(&a0, &a1, &a2);
SleepEx(250, false);
bool err = GetSystemTimes(&b0, &b1, &b2);

if (!err) {
std::cerr << "ERROR IN GetSystemTimes !! Error code 0x" << std::hex
<< GetLastError() << std::endl;
}

printf("IDLE HIGH: %d, IDLE LOW: %d ", b0.dwHighDateTime, b0.dwLowDateTime);
printf("KERN HIGH: %d, KERN LOW: %d ", b1.dwHighDateTime, b1.dwLowDateTime);
printf("USER HIGH: %d, USER LOW: %d\n", b2.dwHighDateTime,
b2.dwLowDateTime);

uint64_t idle0 = FromFileTime(a0);
uint64_t idle1 = FromFileTime(b0);
uint64_t kernel0 = FromFileTime(a1);
uint64_t kernel1 = FromFileTime(b1);
uint64_t user0 = FromFileTime(a2);
uint64_t user1 = FromFileTime(b2);
uint64_t idl = idle1 - idle0;
uint64_t ker = kernel1 - kernel0;
uint64_t usr = user1 - user0;
uint64_t sys0 = kernel0 - idle0;
uint64_t use0 = user0 + sys0;
uint64_t sys1 = kernel1 - idle1;
uint64_t use1 = user1 + sys1;
uint64_t sys = sys1 - sys0;
uint64_t use = use1 - use0;
uint64_t total = ker + usr + idl;
uint64_t cpu_sys = ker - idl; // kernel1 - kernel0 - (idle1 - idle0) ==>
// kernel1 - kernel0 - idle1 + idle0
uint64_t cpu_use = usr + cpu_sys;

utcExample();

PROCESSOR_NUMBER res;
GetCurrentProcessorNumberEx(&res);
// printf(" GROUP: %d | NUMBER: %d |", res.Group, res.Number);
// printf("| %10.4f | %10.4f | %10.4f | %10.4f | %10.4f |\n", use, usr, sys,
// idl + ker, total); printf("| %20llu | %20llu | %20llu |\n", user1,
// kernel1, idle1);

float cpu_usage = ((float)use) * 100.0f / (float)total;
float cpu_user = ((float)usr) * 100.0f / (float)total;
float cpu_system = ((float)sys) * 100.0f / (float)total;
float cpu_idle = ((float)idl) * 100.0f / (float)total;
float cpu_kern = ((float)ker) * 100.0f / (float)total;
float total_percent = cpu_user + cpu_kern + cpu_idle;
float idle_percent = 100 - cpu_kern - cpu_user;

// utcExample();
// printf("| %10.4f | %10.4f | %10.4f | %10.4f | %10.4f |\n", cpu_usage,
// cpu_user, cpu_system, idle_percent, total_percent);

a0 = b0;
a1 = b1;
a2 = b2;

// std::cout << cpu_usage << "| " << cpu_user << " | " << cpu_system <<
// " |" << std::endl;

// return static_cast<float>(cpu);
}
};

int main() {
using namespace std;

Usage Usage;

// for (int i = 0; i < 10; i++)
Usage.utcExample();
// printf("| %10s | %10s | %10s | %10s | %10s |\n", "In Use", "User",
// "System", "Idle", "TOTAL");
printf("| User | Kernel | Idle |\n");
while (true) {
Usage.now();
}

cout << "\nFinished!\nPress any key to exit!\n";
cin.clear();
cin.get();
return 0;
}
85 changes: 85 additions & 0 deletions cmd/debug/cpu_sample.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package main

import (
"fmt"
"io/ioutil"
"log"
"time"

"github.com/newrelic/infrastructure-agent/internal/agent"
"github.com/newrelic/infrastructure-agent/internal/testhelpers"
"github.com/newrelic/infrastructure-agent/pkg/config"
"github.com/newrelic/infrastructure-agent/pkg/metrics"
"github.com/newrelic/infrastructure-agent/pkg/sysinfo/cloud"
"github.com/newrelic/infrastructure-agent/pkg/sysinfo/hostname"
)

var matcher = func(interface{}) bool { return true }

Check failure on line 17 in cmd/debug/cpu_sample.go

View workflow job for this annotation

GitHub Actions / linter-linux / Run Linter

other declaration of matcher

Check failure on line 17 in cmd/debug/cpu_sample.go

View workflow job for this annotation

GitHub Actions / linter-macos / Lint tests

other declaration of matcher

Check failure on line 17 in cmd/debug/cpu_sample.go

View workflow job for this annotation

GitHub Actions / unit-test-macos / unit tests

other declaration of matcher

Check failure on line 17 in cmd/debug/cpu_sample.go

View workflow job for this annotation

GitHub Actions / CodeQL-Build

other declaration of matcher

Check failure on line 17 in cmd/debug/cpu_sample.go

View workflow job for this annotation

GitHub Actions / linter-linux / Run Linter

other declaration of matcher

Check failure on line 17 in cmd/debug/cpu_sample.go

View workflow job for this annotation

GitHub Actions / linter-macos / Lint tests

other declaration of matcher

Check failure on line 17 in cmd/debug/cpu_sample.go

View workflow job for this annotation

GitHub Actions / unit-test-macos / unit tests

other declaration of matcher

Check failure on line 17 in cmd/debug/cpu_sample.go

View workflow job for this annotation

GitHub Actions / CodeQL-Build

other declaration of matcher

func cpu_sample(l *log.Logger, interval *time.Duration) {

Check failure on line 19 in cmd/debug/cpu_sample.go

View workflow job for this annotation

GitHub Actions / linter-linux / Run Linter

other declaration of cpu_sample

Check failure on line 19 in cmd/debug/cpu_sample.go

View workflow job for this annotation

GitHub Actions / linter-macos / Lint tests

other declaration of cpu_sample

Check failure on line 19 in cmd/debug/cpu_sample.go

View workflow job for this annotation

GitHub Actions / unit-test-macos / unit tests

other declaration of cpu_sample

Check failure on line 19 in cmd/debug/cpu_sample.go

View workflow job for this annotation

GitHub Actions / CodeQL-Build

other declaration of cpu_sample

Check failure on line 19 in cmd/debug/cpu_sample.go

View workflow job for this annotation

GitHub Actions / linter-linux / Run Linter

other declaration of cpu_sample

Check failure on line 19 in cmd/debug/cpu_sample.go

View workflow job for this annotation

GitHub Actions / linter-macos / Lint tests

other declaration of cpu_sample

Check failure on line 19 in cmd/debug/cpu_sample.go

View workflow job for this annotation

GitHub Actions / unit-test-macos / unit tests

other declaration of cpu_sample

Check failure on line 19 in cmd/debug/cpu_sample.go

View workflow job for this annotation

GitHub Actions / CodeQL-Build

other declaration of cpu_sample
dataDir, err := ioutil.TempDir("", "prefix")
if err != nil {
panic(err)
}

cfg := config.NewTest(dataDir)

cloudDetector := cloud.NewDetector(true, 0, 0, 0, false)
lookups := agent.NewIdLookup(hostname.CreateResolver("", "", true), cloudDetector, cfg.DisplayName)

ctx := agent.NewContext(cfg, "1.2.3", testhelpers.NullHostnameResolver, lookups, matcher)

cpuMonitor := metrics.NewCPUMonitor(ctx)

// cpuSample, err := cpuMonitor.Sample()
// if err != nil {
// panic(err)
// }

headers := []string{
"In Use", "User", "System",
// "IOWait",
"Idle",
// "Steal"
}
formatterStr := "| %-10s"
formatterNum := "| %-10.4f"
var resH string
var ticker int

for _, v := range headers {
resH += fmt.Sprintf(formatterStr, v)
}

// fmt.Println(resH)

for {
cpuSample, err := cpuMonitor.Sample()
if err != nil {
panic(err)
}
// marshaled, err := json.Marshal(cpuSample)
// if err != nil {
// panic(err)
// }
// fmt.Println(string(marshaled))

if ticker%100 == 0 {
Log(l, resH)

Check failure on line 68 in cmd/debug/cpu_sample.go

View workflow job for this annotation

GitHub Actions / linter-linux / Run Linter

undefined: Log (typecheck)

Check failure on line 68 in cmd/debug/cpu_sample.go

View workflow job for this annotation

GitHub Actions / linter-macos / Lint tests

undefined: Log (typecheck)

Check failure on line 68 in cmd/debug/cpu_sample.go

View workflow job for this annotation

GitHub Actions / unit-test-macos / unit tests

undefined: Log

Check failure on line 68 in cmd/debug/cpu_sample.go

View workflow job for this annotation

GitHub Actions / CodeQL-Build

undefined: Log

Check failure on line 68 in cmd/debug/cpu_sample.go

View workflow job for this annotation

GitHub Actions / linter-linux / Run Linter

undefined: Log (typecheck)

Check failure on line 68 in cmd/debug/cpu_sample.go

View workflow job for this annotation

GitHub Actions / linter-macos / Lint tests

undefined: Log (typecheck)

Check failure on line 68 in cmd/debug/cpu_sample.go

View workflow job for this annotation

GitHub Actions / unit-test-macos / unit tests

undefined: Log

Check failure on line 68 in cmd/debug/cpu_sample.go

View workflow job for this annotation

GitHub Actions / CodeQL-Build

undefined: Log
}

var resN string
resN += fmt.Sprintf(formatterNum, cpuSample.CPUPercent)
resN += fmt.Sprintf(formatterNum, cpuSample.CPUUserPercent)
resN += fmt.Sprintf(formatterNum, cpuSample.CPUSystemPercent)
// resN += fmt.Sprintf(formatterNum, cpuSample.CPUIOWaitPercent)
resN += fmt.Sprintf(formatterNum, cpuSample.CPUIdlePercent)
// resN += fmt.Sprintf(formatterNum, cpuSample.CPUStealPercent)

// Log(l, resN)

time.Sleep(*interval)
ticker++
}

}
85 changes: 85 additions & 0 deletions cmd/debug/main_gopsutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package main

import (
"fmt"
"io/ioutil"
"log"
"time"

"github.com/newrelic/infrastructure-agent/internal/agent"
"github.com/newrelic/infrastructure-agent/internal/testhelpers"
"github.com/newrelic/infrastructure-agent/pkg/config"
"github.com/newrelic/infrastructure-agent/pkg/metrics"
"github.com/newrelic/infrastructure-agent/pkg/sysinfo/cloud"
"github.com/newrelic/infrastructure-agent/pkg/sysinfo/hostname"
)

var matcher = func(interface{}) bool { return true }

Check failure on line 17 in cmd/debug/main_gopsutil.go

View workflow job for this annotation

GitHub Actions / linter-linux / Run Linter

matcher redeclared in this block

Check failure on line 17 in cmd/debug/main_gopsutil.go

View workflow job for this annotation

GitHub Actions / linter-macos / Lint tests

matcher redeclared in this block

Check failure on line 17 in cmd/debug/main_gopsutil.go

View workflow job for this annotation

GitHub Actions / unit-test-macos / unit tests

matcher redeclared in this block

Check failure on line 17 in cmd/debug/main_gopsutil.go

View workflow job for this annotation

GitHub Actions / CodeQL-Build

matcher redeclared in this block

Check failure on line 17 in cmd/debug/main_gopsutil.go

View workflow job for this annotation

GitHub Actions / linter-linux / Run Linter

matcher redeclared in this block

Check failure on line 17 in cmd/debug/main_gopsutil.go

View workflow job for this annotation

GitHub Actions / linter-macos / Lint tests

matcher redeclared in this block

Check failure on line 17 in cmd/debug/main_gopsutil.go

View workflow job for this annotation

GitHub Actions / unit-test-macos / unit tests

matcher redeclared in this block

Check failure on line 17 in cmd/debug/main_gopsutil.go

View workflow job for this annotation

GitHub Actions / CodeQL-Build

matcher redeclared in this block

func cpu_sample(l *log.Logger, interval *time.Duration) {

Check failure on line 19 in cmd/debug/main_gopsutil.go

View workflow job for this annotation

GitHub Actions / linter-linux / Run Linter

cpu_sample redeclared in this block

Check failure on line 19 in cmd/debug/main_gopsutil.go

View workflow job for this annotation

GitHub Actions / linter-macos / Lint tests

cpu_sample redeclared in this block

Check failure on line 19 in cmd/debug/main_gopsutil.go

View workflow job for this annotation

GitHub Actions / unit-test-macos / unit tests

cpu_sample redeclared in this block

Check failure on line 19 in cmd/debug/main_gopsutil.go

View workflow job for this annotation

GitHub Actions / CodeQL-Build

cpu_sample redeclared in this block

Check failure on line 19 in cmd/debug/main_gopsutil.go

View workflow job for this annotation

GitHub Actions / linter-linux / Run Linter

cpu_sample redeclared in this block

Check failure on line 19 in cmd/debug/main_gopsutil.go

View workflow job for this annotation

GitHub Actions / linter-macos / Lint tests

cpu_sample redeclared in this block

Check failure on line 19 in cmd/debug/main_gopsutil.go

View workflow job for this annotation

GitHub Actions / unit-test-macos / unit tests

cpu_sample redeclared in this block

Check failure on line 19 in cmd/debug/main_gopsutil.go

View workflow job for this annotation

GitHub Actions / CodeQL-Build

cpu_sample redeclared in this block
dataDir, err := ioutil.TempDir("", "prefix")
if err != nil {
panic(err)
}

cfg := config.NewTest(dataDir)

cloudDetector := cloud.NewDetector(true, 0, 0, 0, false)
lookups := agent.NewIdLookup(hostname.CreateResolver("", "", true), cloudDetector, cfg.DisplayName)

ctx := agent.NewContext(cfg, "1.2.3", testhelpers.NullHostnameResolver, lookups, matcher)

cpuMonitor := metrics.NewCPUMonitor(ctx)

// cpuSample, err := cpuMonitor.Sample()
// if err != nil {
// panic(err)
// }

headers := []string{
"In Use", "User", "System",
// "IOWait",
"Idle",
// "Steal"
}
formatterStr := "| %-10s"
formatterNum := "| %-10.4f"
var resH string
var ticker int

for _, v := range headers {
resH += fmt.Sprintf(formatterStr, v)
}

// fmt.Println(resH)

for {
cpuSample, err := cpuMonitor.Sample()
if err != nil {
panic(err)
}
// marshaled, err := json.Marshal(cpuSample)
// if err != nil {
// panic(err)
// }
// fmt.Println(string(marshaled))

if ticker%100 == 0 {
Log(l, resH)

Check failure on line 68 in cmd/debug/main_gopsutil.go

View workflow job for this annotation

GitHub Actions / linter-linux / Run Linter

undefined: Log

Check failure on line 68 in cmd/debug/main_gopsutil.go

View workflow job for this annotation

GitHub Actions / linter-macos / Lint tests

undefined: Log

Check failure on line 68 in cmd/debug/main_gopsutil.go

View workflow job for this annotation

GitHub Actions / unit-test-macos / unit tests

undefined: Log

Check failure on line 68 in cmd/debug/main_gopsutil.go

View workflow job for this annotation

GitHub Actions / CodeQL-Build

undefined: Log

Check failure on line 68 in cmd/debug/main_gopsutil.go

View workflow job for this annotation

GitHub Actions / linter-linux / Run Linter

undefined: Log

Check failure on line 68 in cmd/debug/main_gopsutil.go

View workflow job for this annotation

GitHub Actions / linter-macos / Lint tests

undefined: Log

Check failure on line 68 in cmd/debug/main_gopsutil.go

View workflow job for this annotation

GitHub Actions / unit-test-macos / unit tests

undefined: Log

Check failure on line 68 in cmd/debug/main_gopsutil.go

View workflow job for this annotation

GitHub Actions / CodeQL-Build

undefined: Log
}

var resN string
resN += fmt.Sprintf(formatterNum, cpuSample.CPUPercent)
resN += fmt.Sprintf(formatterNum, cpuSample.CPUUserPercent)
resN += fmt.Sprintf(formatterNum, cpuSample.CPUSystemPercent)
// resN += fmt.Sprintf(formatterNum, cpuSample.CPUIOWaitPercent)
resN += fmt.Sprintf(formatterNum, cpuSample.CPUIdlePercent)
// resN += fmt.Sprintf(formatterNum, cpuSample.CPUStealPercent)

// Log(l, resN)

time.Sleep(*interval)
ticker++
}

}
Binary file added cmd/debug/windows_amd64.exe
Binary file not shown.
Loading