问题
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;
}
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]
^
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.
回答1:
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
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.
来源:https://stackoverflow.com/questions/61774157/dereference-error-when-accessing-slurm-job-resources-using-c-api