使用 C API 访问 Slurm 作业资源时取消引用错误 [英] Dereference error when accessing Slurm job resources using C API

查看:46
本文介绍了使用 C API 访问 Slurm 作业资源时取消引用错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 C API 获取 Slurm 集群中每个作业的内存使用信息:

I am trying to get memory usage information for each job in the Slurm cluster using C API:

#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)时,我收到以下错误:

When I compile this code (saved as jobres.c) I am getting the following errors:

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]
                                                      ^

-> 更改为 . 并不能解决问题并产生不同的错误:

Changing -> to . does not solve the problem and produces different errors:

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]
                                                      ^

我在 https 上看到一些 Slurm 工具和插件的源代码中以类似的方式使用作业资源结构://github.com/SchedMD/slurm,但显然我一定遗漏了一些东西,因为我的代码甚至无法编译.我将不胜感激对此问题的有见地的评论或回答.

I saw job resources structures being used in a similar way in the source code of some Slurm tools and plugins at https://github.com/SchedMD/slurm, but apparently I must be missing something as my code does not even compile. I will be grateful for insightful comments or answers on this issue.

推荐答案

缺少标题是有用的提示.在 slurm.h 中,作业资源是一种不透明的数据类型,因为它在第 83 行读取:

Missing header was the helpful hint. In slurm.h job resources is an opaque data type as it reads at line 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 复制了结构定义并将其粘贴到我的程序代码中:

The complete definition can be found in job_resources.h which is the part of Slurm source code, but not the part of API. I copied the structure definition from job_resources.h and pasted it into my program's code:

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

现在这个程序编译没有错误并且运行良好,结果被正确打印出来.

Now this program compiles without errors and runs fine with the results being printed out correctly.

这篇关于使用 C API 访问 Slurm 作业资源时取消引用错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆