Merge Go frontend (apiserver + xcecli) into meson build
This commit is contained in:
parent
43dc12a36c
commit
c9dc8c4676
205 changed files with 30793 additions and 328 deletions
210
src/frontend/apiserver/backend/convert.go
Normal file
210
src/frontend/apiserver/backend/convert.go
Normal file
|
|
@ -0,0 +1,210 @@
|
|||
//go:build cgo
|
||||
|
||||
package backend
|
||||
|
||||
/*
|
||||
#include <stdlib.h>
|
||||
#include "client.h"
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"apiserver/models"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// goString safely converts a C string to a Go string, handling nil.
|
||||
func goString(s *C.char) string {
|
||||
if s == nil {
|
||||
return ""
|
||||
}
|
||||
return C.GoString(s)
|
||||
}
|
||||
|
||||
// cJobToModel extracts C xce_job_t fields into a rawJobData and maps it to JobInfo.
|
||||
func cJobToModel(job *C.xce_job_t) *models.JobInfo {
|
||||
r := rawJobData{
|
||||
jobID: int64(job.jobid),
|
||||
state: int(job.state),
|
||||
runTime: int(job.run_time),
|
||||
subTime: int64(job.sub_time),
|
||||
queueTime: int64(job.queue_time),
|
||||
startTime: int64(job.start_time),
|
||||
finishTime: int64(job.finish_time),
|
||||
}
|
||||
if job.submit != nil {
|
||||
s := job.submit
|
||||
rd := &rawSubmitData{
|
||||
queue: goString(s.queue),
|
||||
user: goString(s.user),
|
||||
jobName: goString(s.job_name),
|
||||
projectName: goString(s.project_name),
|
||||
userGroup: goString(s.user_group),
|
||||
jobDesc: goString(s.job_description),
|
||||
resReq: goString(s.resreq),
|
||||
minTasks: int(s.mintasks),
|
||||
maxTasks: int(s.maxtasks),
|
||||
}
|
||||
if s.start_info != nil {
|
||||
si := s.start_info
|
||||
rd.startInfo = &rawStartInfo{
|
||||
cmdline: goString(si.cmdline),
|
||||
launchType: goString(si.launch_type),
|
||||
preExec: goString(si.job_pre_exec),
|
||||
postExec: goString(si.job_post_exec),
|
||||
execHome: goString(si.exechome),
|
||||
execCWD: goString(si.execcwd),
|
||||
execUser: goString(si.execuser),
|
||||
}
|
||||
}
|
||||
r.submit = rd
|
||||
}
|
||||
return rawJobToModel(r)
|
||||
}
|
||||
|
||||
// cWorkerToModel extracts C xce_worker_t fields into a rawWorkerData and maps it to HostInfo.
|
||||
func cWorkerToModel(worker *C.xce_worker_t) *models.HostInfo {
|
||||
r := rawWorkerData{
|
||||
name: goString(worker.name),
|
||||
status: int(worker.status),
|
||||
}
|
||||
if worker.attrs != nil {
|
||||
a := worker.attrs
|
||||
r.hasAttrs = true
|
||||
r.hostType = goString(a.hosttype)
|
||||
r.hostModel = goString(a.hostmodel)
|
||||
r.cpuFactor = float64(a.cpufactor)
|
||||
r.maxCPUs = int(a.maxcpus)
|
||||
r.maxMem = int(a.maxmem)
|
||||
r.maxSwap = int(a.maxswap)
|
||||
r.maxTmp = int(a.maxtmp)
|
||||
r.nDisks = int(a.ndisks)
|
||||
}
|
||||
return rawWorkerToModel(r)
|
||||
}
|
||||
|
||||
// cQueueToModel extracts C xce_queue_t fields and maps them to QueueInfo.
|
||||
func cQueueToModel(queue *C.xce_queue_t) *models.QueueInfo {
|
||||
return rawQueueToModel(rawQueueData{
|
||||
name: goString(queue.queue),
|
||||
status: int(queue.status),
|
||||
priority: int(queue.priority),
|
||||
})
|
||||
}
|
||||
|
||||
// cResourceToModel extracts C xce_resource_t fields and maps them to ResourceDefinitionInfo.
|
||||
func cResourceToModel(res *C.xce_resource_t) *models.ResourceDefinitionInfo {
|
||||
return rawResourceToModel(rawResourceData{
|
||||
name: goString(res.name),
|
||||
desc: goString(res.desc),
|
||||
valType: int(res.valtype),
|
||||
ordType: int(res.ordtype),
|
||||
flags: int(res.flags),
|
||||
interval: int(res.interval),
|
||||
})
|
||||
}
|
||||
|
||||
// cClusterConfQueues extracts queue models from a cluster_config_t.
|
||||
func cClusterConfQueues(conf *C.cluster_config_t) []models.QueueInfo {
|
||||
n := int(conf.nqueues)
|
||||
if n == 0 {
|
||||
return nil
|
||||
}
|
||||
qps := unsafe.Slice(conf.queues, n)
|
||||
out := make([]models.QueueInfo, 0, n)
|
||||
for _, qp := range qps {
|
||||
if qp != nil {
|
||||
out = append(out, *cQueueToModel(qp))
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// cClusterConfResources extracts resource definition models from a cluster_config_t.
|
||||
func cClusterConfResources(conf *C.cluster_config_t) []models.ResourceDefinitionInfo {
|
||||
n := int(conf.nres)
|
||||
if n == 0 {
|
||||
return nil
|
||||
}
|
||||
rps := unsafe.Slice(conf.resources, n)
|
||||
out := make([]models.ResourceDefinitionInfo, 0, n)
|
||||
for _, rp := range rps {
|
||||
if rp != nil {
|
||||
out = append(out, *cResourceToModel(rp))
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// modelJobToSubmit converts a Go JobInfo to C xce_job_submit_t heap structs.
|
||||
// The caller must free all returned C memory by calling freeJobSubmit.
|
||||
func modelJobToSubmit(job *models.JobInfo) (*C.xce_job_submit_t, *C.job_submit_data_t, *C.job_startinfo_t) {
|
||||
si := (*C.job_startinfo_t)(C.calloc(1, C.sizeof_job_startinfo_t))
|
||||
si.cmdline = C.CString(job.Command)
|
||||
if job.LaunchType != "" {
|
||||
si.launch_type = C.CString(job.LaunchType)
|
||||
}
|
||||
if job.PreExecCmd != "" {
|
||||
si.job_pre_exec = C.CString(job.PreExecCmd)
|
||||
}
|
||||
if job.PostExecCmd != "" {
|
||||
si.job_post_exec = C.CString(job.PostExecCmd)
|
||||
}
|
||||
|
||||
sd := (*C.job_submit_data_t)(C.calloc(1, C.sizeof_job_submit_data_t))
|
||||
sd.queue = C.CString(job.QueueName)
|
||||
sd.user = C.CString(job.SubmitUser)
|
||||
sd.job_name = C.CString(job.JobName)
|
||||
if job.ProjectName != "" {
|
||||
sd.project_name = C.CString(job.ProjectName)
|
||||
}
|
||||
if job.UserGroup != "" {
|
||||
sd.user_group = C.CString(job.UserGroup)
|
||||
}
|
||||
if job.JobDescription != "" {
|
||||
sd.job_description = C.CString(job.JobDescription)
|
||||
}
|
||||
if job.ResReq != "" {
|
||||
sd.resreq = C.CString(job.ResReq)
|
||||
}
|
||||
sd.mintasks = C.uint(job.NumProcessors)
|
||||
if sd.mintasks == 0 {
|
||||
sd.mintasks = 1
|
||||
}
|
||||
sd.maxtasks = C.uint(job.MaxNumProcessors)
|
||||
if sd.maxtasks < sd.mintasks {
|
||||
sd.maxtasks = sd.mintasks
|
||||
}
|
||||
sd.start_info = si
|
||||
|
||||
js := (*C.xce_job_submit_t)(C.calloc(1, C.sizeof_xce_job_submit_t))
|
||||
js.jobid = C.uint64_t(job.Unique_JobID)
|
||||
js.array_size = 1
|
||||
js.submit = sd
|
||||
|
||||
return js, sd, si
|
||||
}
|
||||
|
||||
// freeJobSubmit releases C memory allocated by modelJobToSubmit.
|
||||
func freeJobSubmit(js *C.xce_job_submit_t, sd *C.job_submit_data_t, si *C.job_startinfo_t) {
|
||||
if si != nil {
|
||||
C.free(unsafe.Pointer(si.cmdline))
|
||||
C.free(unsafe.Pointer(si.launch_type))
|
||||
C.free(unsafe.Pointer(si.job_pre_exec))
|
||||
C.free(unsafe.Pointer(si.job_post_exec))
|
||||
C.free(unsafe.Pointer(si))
|
||||
}
|
||||
if sd != nil {
|
||||
C.free(unsafe.Pointer(sd.queue))
|
||||
C.free(unsafe.Pointer(sd.user))
|
||||
C.free(unsafe.Pointer(sd.job_name))
|
||||
C.free(unsafe.Pointer(sd.project_name))
|
||||
C.free(unsafe.Pointer(sd.user_group))
|
||||
C.free(unsafe.Pointer(sd.job_description))
|
||||
C.free(unsafe.Pointer(sd.resreq))
|
||||
C.free(unsafe.Pointer(sd))
|
||||
}
|
||||
if js != nil {
|
||||
C.free(unsafe.Pointer(js))
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue