Getting program metadata from your own program

how can I bring the following values ​​to my C program variables:

  • CPU used to execute the program, i.e. how much processor is spent on the same program.

  • Program execution time, i.e. how long it took to complete.

  • Compiler warnings i.e. How can I put compiler warnings in string variables in my own program?

  • The size of my program on disk: the program spends my hard drive.

It’s very difficult for me to do this, and I don’t know how to do it.

Thank you all in advance

+4
source share
2 answers

Executable compiler warnings are information that is only available after creating your program. Therefore, I think that it is not easy to collect this information statically to your "C program variables."

You can save this data to a file as an additional build step. For example, write a program that executes the compiler and reads its output. This program will then either save the data in a file or provide them with a linker and tell it to pack it as a ressource (but then you didn’t have the linker warnings).

Size, processor usage, and runtime are information that can be selected by the program at runtime. You can easily get the file size using the C library (fopen, etc.). You can get the start time by starting the timer when the application starts and right before you exit it, read this timer to get the total operating time. I think that to use the processor you should ask the operating system (a quick google search for windows made me this )

0
source

[EDITED: inclusion of program runtime, file size]

For windows only: here is some code that you can use to get what you want. This implementation only returns PeakWorkingSize, but I included a commented-out copy of the structure containing all the values ​​you can get, with minor changes. This will compile and build in ANSI C if you enable psapi.lib (part of the Windows SDK installation, free download here )

#include <windows.h> #include <ansi_c.h> #include <psapi.h> time_t GetMemUsage(void); int main(int argc, char *argv[]) { DWORD start, elapsed; // for program execution time size_t memory; //for cpu usage; DWORD filesize=0; //for exe file size FILE *fp; char buf[260]; int i; start = GetTickCount(); sprintf(buf, ".\\%s", argv[0]); fp = fopen(buf, "r"); filesize = GetFileSize(fp, NULL); for(i=0;i<1000000;i++); //so ticks will be more than zero memory = GetMemUsage(); fclose(fp); elapsed = GetTickCount() - start; //note, possible rollover, return 0; } time_t GetMemUsage(void) { HANDLE hProcess; PROCESS_MEMORY_COUNTERS pmc; DWORD processID = GetCurrentProcessId(); hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID ); GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)); CloseHandle(hProcess); // typedef struct _PROCESS_MEMORY_COUNTERS { // DWORD cb; // DWORD PageFaultCount; // SIZE_T PeakWorkingSetSize; // SIZE_T WorkingSetSize; // SIZE_T QuotaPeakPagedPoolUsage; // SIZE_T QuotaPagedPoolUsage; // SIZE_T QuotaPeakNonPagedPoolUsage; // SIZE_T QuotaNonPagedPoolUsage; // SIZE_T PagefileUsage; // SIZE_T PeakPagefileUsage; // } PROCESS_MEMORY_COUNTERS; typedef PROCESS_MEMORY_COUNTERS *PPROCESS_MEMORY_COUNTERS; return pmc.PeakWorkingSetSize; } 
0
source

Source: https://habr.com/ru/post/1500970/


All Articles