简体   繁体   English

从自己的程序获取程序元数据

[英]Getting program metadata from own program

how can I bring to my C program variables the following values​​: 如何为我的C程序变量带来以下值:

  • CPU used for the execution of the program, ie, how much processor is spent on that same program. 用于执行程序的CPU,即在同一程序上花费了多少处理器。

  • The execution time of the program, ie, how long it took to be completed. 程序的执行时间,即完成所需的时间。

  • The compiler warnings, ie, how can I put compiler warnings on string variables in my own program? 编译器警告,即如何在我自己的程序中对字符串变量放置编译器警告?

  • The size of my program to disk: the program spend my hard disk. 我的程序到磁盘的大小:程序花在我的硬盘上。

I find this very difficult to do and I do not know any way of doing it. 我发现这很难做到,我不知道如何做到这一点。

Thanks to all in advance 感谢所有提前

The compiler warnings of the executable is information that is only available after your program has been built. 可执行文件的编译器警告是仅在构建程序后可用的信息。 So I think it is not easily possible to pack this information statically to your "C program variables". 所以我认为不可能将这些信息静态地打包到你的“C程序变量”中。

You could 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 would then either save the data to a file or give it the linker and tell it to pack it as ressource (but then you would be missing linker warnings). 然后,该程序将数据保存到文件或为其提供链接器并告诉它将其打包为ressource(但随后您将丢失链接器警告)。

Size, cpu usage and run time is information that can be fetched by the program at runtime. 大小,cpu使用和运行时间是程序在运行时可以获取的信息。 You can get the size of a file easily with the C library (fopen etc). 您可以使用C库(fopen等)轻松获取文件的大小。 Run time can be gotten by starting a timer at the startup of your application and right before exiting you read that timer to get the total run time. 可以通过在应用程序启动时启动计时器并在退出之前读取该计时器以获得总运行时间来获取运行时间。 I think for cpu usage you have to ask the operating system (a quick google search for windows got me this ) 我认为对于cpu使用你必须要求操作系统(快速谷歌搜索Windows让我这样

[EDITED to include program execution time, filesize] [已编辑包含程序执行时间,文件大小]

For windows only: here is some code that can be used to get some of what you want . 仅适用于Windows:这里有一些代码可用于获取您想要的内容 This implementation returns only PeakWorkingSize, but I have included a commented copy of the struct containing all of the values you can obtain, with minor modifications. 此实现仅返回PeakWorkingSize,但我已经包含了一个注释的结构副本,其中包含您可以获得的所有值,只需稍作修改即可。 This will compile and build in ANSI C if you include the psapi.lib (part of windows SDK installation, freely down loadable here ) 如果你包含psapi.lib(windows SDK安装的一部分, 这里可以自由下载),这将编译和构建ANSI C

#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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM