使用 C API 访问 Slurm 作业资源时出现 Dereference 错误。

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

我试图使用C API获取Slurm集群中每个作业的内存使用信息。

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

int main(int argc, char** argv)
{
        int c, i, slurm_err;
        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++)
        {
                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,
                        jobs->job_array[i].job_resrcs->memory_allocated[0],
                        jobs->job_array[i].job_resrcs->memory_used[0]
                );
        }
        slurm_free_job_info_msg(jobs);
        return 0;
}

当我编译这段代码(保存为jobres.c)时,我得到以下错误。

jobres.c: In function ‘main’:
jobres.c:34:54: error: dereferencing pointer to incomplete type
                         jobs->job_array[i].job_resrcs->memory_allocated[0],
                                                      ^
jobres.c:35:54: error: dereferencing pointer to incomplete type
                         jobs->job_array[i].job_resrcs->memory_used[0]
                                                      ^

改变 ->. 并不能解决问题,还会产生不同的错误。

jobres.c: In function ‘main’:
jobres.c:34:54: error: request for member ‘memory_allocated’ in something not a structure or union
                         jobs->job_array[i].job_resrcs.memory_allocated[0],
                                                      ^
jobres.c:35:54: error: request for member ‘memory_used’ in something not a structure or union
                         jobs->job_array[i].job_resrcs.memory_used[0]
                                                      ^

我在一些Slurm工具和插件的源代码中看到了类似的工作资源结构的使用。https:/github.comSchedMDslurm。但显然我一定是遗漏了什么,因为我的代码甚至无法编译。如果您能对这个问题提出有见地的意见或答案,我将感激不尽。

c api slurm
1个回答
1
投票

缺少的头是有用的提示。在 slurm.h job resources是一个不透明的数据类型,因为它在第83行读到。

/* Define job_resources_t below
 * to avoid including extraneous slurm headers */
#ifndef __job_resources_t_defined
#  define  __job_resources_t_defined    /* Opaque data for select plugins */
typedef struct job_resources job_resources_t;
#endif

完整的定义可以在 job_resources.h 这是Slurm源代码的一部分,但不是API的一部分。我把结构定义从 job_resources.h 并将其粘贴到我的程序代码中。

#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 c, i, slurm_err;
        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++)
        {
                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,
                        jobs->job_array[i].job_resrcs->memory_allocated[0],
                        jobs->job_array[i].job_resrcs->memory_used[0]
                );
        }
        slurm_free_job_info_msg(jobs);
        return 0;
}

现在这个程序在编译时没有错误,运行也很正常,结果也能正确地打印出来。

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