如何使用Slurm C API获取内存使用信息?

问题描述 投票:0回答:1

我正在寻找使用C API从Slurm获取每个作业的内存使用情况信息的方法,即已使用的内存已保留的内存。我以为可以通过调用slurm_load_jobs(…)来获得此类统计信息,但是查看job_step_info_t类型定义时,我看不到任何相关字段。可能在job_resrcs中可能有某些内容,但是它是不透明的数据类型,我不知道如何使用它。还是有另一个API调用可以给我详细的内存使用信息?请指教。

c api memory hpc slurm
1个回答
0
投票

this SO thread中部分回答了此问题,其中的重点仅在于编译器错误。代码的缺失部分是memory_allocatedmemory_user数组的循环,该数组的大小取决于将作业分派到的主机数:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "slurm/slurm.h"
#include "slurm/slurm_errno.h"


struct job_resources {
        bitstr_t *core_bitmap;
        bitstr_t *core_bitmap_used;
        uint32_t  cpu_array_cnt;
        uint16_t *cpu_array_value;
        uint32_t *cpu_array_reps;
        uint16_t *cpus;
        uint16_t *cpus_used;
        uint16_t *cores_per_socket;
        uint64_t *memory_allocated;
        uint64_t *memory_used;
        uint32_t  nhosts;
        bitstr_t *node_bitmap;
        uint32_t  node_req;
        char     *nodes;
        uint32_t  ncpus;
        uint32_t *sock_core_rep_count;
        uint16_t *sockets_per_node;
        uint16_t *tasks_per_node;
        uint8_t   whole_node;

};

int main(int argc, char** argv)
{
        int i, j, slurm_err;
        uint64_t mem_alloc, mem_used;
        job_info_msg_t *jobs;

        /* Load job info from Slurm */
        slurm_err = slurm_load_jobs((time_t) NULL, &jobs, SHOW_DETAIL);
        printf("job_id,cluster,partition,user_id,name,job_state,mem_allocated,mem_used\n");
        /* Print jobs info to the file in CSV format */
        for (i = 0; i < jobs->record_count; i++)
        {
                mem_alloc = 0;
                mem_used = 0;
                for (j = 0; j < jobs->job_array[i].job_resrcs->nhosts; j++)
                {
                        mem_alloc += jobs->job_array[i].job_resrcs->memory_allocated[j];
                        mem_used  += jobs->job_array[i].job_resrcs->memory_used[0];
                }
                printf("%d,%s,%s,%d,%s,%d,%d,%d\n",
                        jobs->job_array[i].job_id,
                        jobs->job_array[i].cluster,
                        jobs->job_array[i].partition,
                        jobs->job_array[i].user_id,
                        jobs->job_array[i].name,
                        jobs->job_array[i].job_state,
                        mem_alloc,
                        mem_used
                );
        }
        slurm_free_job_info_msg(jobs);
        return 0;
}

该程序可以编译并运行,没有错误。我注意到的一件事是mem_used等于0或等于mem_alloc,有时与我从sstat命令获得的内容有所不同。我将不得不对此进行进一步调查...

© www.soinside.com 2019 - 2024. All rights reserved.