/
jstatctl.c
1300 lines (1168 loc) · 37.2 KB
/
jstatctl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************\
* Copyright (c) 2015 Lawrence Livermore National Security, LLC. Produced at
* the Lawrence Livermore National Laboratory (cf, AUTHORS, DISCLAIMER.LLNS).
* LLNL-CODE-658032 All rights reserved.
*
* This file is part of the Flux resource manager framework.
* For details, see https://github.com/flux-framework.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the license, or (at your option)
* any later version.
*
* Flux is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the IMPLIED WARRANTY OF MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the terms and conditions of the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
* See also: http://www.gnu.org/licenses/
\*****************************************************************************/
#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <czmq.h>
#include <stdint.h>
#include <string.h>
#include <limits.h>
#include <stdbool.h>
#include <inttypes.h>
#include <flux/core.h>
#include "jstatctl.h"
#include "src/common/libutil/log.h"
#include "src/common/libutil/xzmalloc.h"
#include "src/common/libutil/iterators.h"
#include "src/common/libutil/shortjson.h"
#include "src/common/libutil/lru_cache.h"
/*******************************************************************************
* *
* Internal User Define Types and Data *
* *
*******************************************************************************/
typedef struct {
job_state_t i;
const char *s;
} stab_t;
typedef struct {
jsc_handler_f cb;
void *arg;
} cb_pair_t;
typedef struct {
zhash_t *active_jobs;
lru_cache_t *kvs_paths;
flux_msg_handler_t **handlers;
zlist_t *callbacks;
flux_t *h;
} jscctx_t;
static stab_t job_state_tab[] = {
{ J_NULL, "null" },
{ J_RESERVED, "reserved" },
{ J_SUBMITTED, "submitted" },
{ J_PENDING, "pending" },
{ J_SCHEDREQ, "schedreq" },
{ J_SELECTED, "selected" },
{ J_ALLOCATED, "allocated" },
{ J_RUNREQUEST, "runrequest" },
{ J_STARTING, "starting" },
{ J_STOPPED, "stopped" },
{ J_RUNNING, "running" },
{ J_CANCELLED, "cancelled" },
{ J_COMPLETE, "complete" },
{ J_REAPED, "reaped" },
{ J_FAILED, "failed" },
{ J_FOR_RENT, "for_rent" },
{ -1, NULL },
};
/******************************************************************************
* *
* Utilities *
* *
******************************************************************************/
const char *jsc_job_num2state (job_state_t i)
{
stab_t *ss = job_state_tab;
while (ss->s != NULL) {
if (ss->i == i)
return ss->s;
ss++;
}
return NULL;
}
int jsc_job_state2num (const char *s)
{
stab_t *ss = job_state_tab;
while (ss->s != NULL) {
if (!strcmp (ss->s, s))
return ss->i;
ss++;
}
return -1;
}
static void freectx (void *arg)
{
jscctx_t *ctx = arg;
zhash_destroy (&(ctx->active_jobs));
lru_cache_destroy (ctx->kvs_paths);
zlist_destroy (&(ctx->callbacks));
flux_msg_handler_delvec (ctx->handlers);
}
static jscctx_t *getctx (flux_t *h)
{
jscctx_t *ctx = (jscctx_t *)flux_aux_get (h, "jstatctrl");
if (!ctx) {
ctx = xzmalloc (sizeof (*ctx));
if (!(ctx->active_jobs = zhash_new ()))
oom ();
if (!(ctx->kvs_paths = lru_cache_create (256)))
oom ();
lru_cache_set_free_f (ctx->kvs_paths, free);
if (!(ctx->callbacks = zlist_new ()))
oom ();
ctx->h = h;
flux_aux_set (h, "jstatctrl", ctx, freectx);
}
return ctx;
}
json_object * kvspath_request_json (int64_t id)
{
json_object *o = json_object_new_object ();
json_object *ar = json_object_new_array ();
json_object *v = json_object_new_int64 (id);
if (!o || !ar || !v) {
Jput (o);
Jput (ar);
Jput (v);
return (NULL);
}
json_object_array_add (ar, v);
json_object_object_add (o, "ids", ar);
return (o);
}
const char * kvs_path_json_get (json_object *o)
{
const char *p;
json_object *ar;
if (!Jget_obj (o, "paths", &ar) || !Jget_ar_str (ar, 0, &p))
return (NULL);
return (p);
}
static int lwj_kvs_path (flux_t *h, int64_t id, char **pathp)
{
int rc = -1;
const char *path;
const char *json_str;
flux_future_t *f;
uint32_t nodeid = FLUX_NODEID_ANY;
json_object *o = kvspath_request_json (id);
if (!(f = flux_rpc (h, "job.kvspath", Jtostr (o), nodeid, 0))
|| (flux_rpc_get (f, &json_str) < 0)) {
flux_log_error (h, "flux_rpc (job.kvspath)");
goto out;
}
Jput (o);
o = NULL;
if (!json_str) {
flux_log (h, LOG_ERR, "flux_rpc (job.kvspath): empty payload");
goto out;
}
if (!(o = Jfromstr (json_str))) {
flux_log_error (h, "flux_rpc (job.kvspath): failed to parse json");
goto out;
}
if (!(path = kvs_path_json_get (o))) {
flux_log_error (h, "flux_rpc (job.kvspath): failed to get path");
goto out;
}
if (!(*pathp = strdup (path))) {
flux_log_error (h, "flux_rpc (job.kvspath): strdup");
goto out;
}
rc = 0;
out:
flux_future_destroy (f);
Jput (o);
return (rc);
}
static int jscctx_add_jobid_path (jscctx_t *ctx, int64_t id, const char *path)
{
char *s;
char key [21];
memset (key, 0, sizeof (key));
if (sprintf (key, "%"PRId64, id) < 0)
return (-1);
if (!(s = strdup (path)))
return (-1);
if (lru_cache_put (ctx->kvs_paths, key, s) < 0) {
if (errno != EEXIST)
flux_log_error (ctx->h, "jscctx_add_job_path");
free (s);
}
return (0);
}
static const char * jscctx_jobid_path (jscctx_t *ctx, int64_t id)
{
char *path;
char key [21];
memset (key, 0, sizeof (key));
if (sprintf (key, "%"PRId64, id) < 0)
return (NULL);
if ((path = lru_cache_get (ctx->kvs_paths, key)))
return path;
if (lwj_kvs_path (ctx->h, id, &path) < 0)
return (NULL);
if (lru_cache_put (ctx->kvs_paths, key, path) < 0)
return (NULL);
return (path);
}
static int fetch_and_update_state (zhash_t *aj , int64_t j, int64_t ns)
{
int *t = NULL;
char *key = NULL;
if (!aj) return J_FOR_RENT;
key = xasprintf ("%"PRId64, j);
if ( !(t = ((int *)zhash_lookup (aj, (const char *)key)))) {
free (key);
return J_FOR_RENT;
}
if (ns == J_COMPLETE || ns == J_FAILED)
zhash_delete (aj, key);
else
zhash_update (aj, key, (void *)(intptr_t)ns);
free (key);
/* safe to convert t to int */
return (intptr_t) t;
}
/******************************************************************************
* *
* Internal JCB Accessors *
* *
******************************************************************************/
static int jobid_exist (flux_t *h, int64_t j)
{
flux_future_t *f = NULL;
jscctx_t *ctx = getctx (h);
const char *path = jscctx_jobid_path (ctx, j);
int rc = -1;
if (path == NULL)
goto done;
if (!(f = flux_kvs_lookup (h, FLUX_KVS_READDIR, path))
|| flux_kvs_lookup_get_dir (f, NULL) < 0) {
flux_log (h, LOG_DEBUG, "flux_kvs_lookup(%s): %s",
path, flux_strerror (errno));
goto done;
}
rc = 0;
done:
flux_future_destroy (f);
return rc;
}
static bool fetch_rank_pdesc (json_object *src, int64_t *p, int64_t *n,
const char **c)
{
if (!src) return false;
if (!Jget_str (src, "command", c)) return false;
if (!Jget_int64 (src, "pid", p)) return false;
if (!Jget_int64 (src, "nodeid", n)) return false;
return true;
}
static int build_name_array (zhash_t *ha, const char *k, json_object *ns)
{
int i = (intptr_t) zhash_lookup (ha, k);
if ((void *)((intptr_t)i) == NULL) {
char *t = xstrdup (k);
i = json_object_array_length (ns);
Jadd_ar_str (ns, t);
zhash_insert (ha, k, (void *)(intptr_t)i+1);
free (t);
} else
i--;
return i;
}
static char * lwj_key (flux_t *h, int64_t id, const char *fmt, ...)
{
int rc;
va_list ap;
jscctx_t *ctx = getctx (h);
const char *base;
char *p = NULL;
char *key;
if (!(base = jscctx_jobid_path (ctx, id)))
return (NULL);
if (fmt != NULL) {
va_start (ap, fmt);
rc = vasprintf (&p, fmt, ap);
va_end (ap);
if (rc < 0)
return (NULL);
}
if (asprintf (&key, "%s%s", base, p) < 0)
return (NULL);
free (p);
return (key);
}
static int extract_raw_nnodes (flux_t *h, int64_t j, int64_t *nnodes)
{
int rc = 0;
char *key = lwj_key (h, j, ".nnodes");
flux_future_t *f = NULL;
if (!key || !(f = flux_kvs_lookup (h, 0, key))
|| flux_kvs_lookup_get_unpack (f, "I", nnodes) < 0) {
flux_log_error (h, "extract %s", key);
rc = -1;
}
else
flux_log (h, LOG_DEBUG, "extract %s: %"PRId64"", key, *nnodes);
free (key);
flux_future_destroy (f);
return rc;
}
static int extract_raw_ntasks (flux_t *h, int64_t j, int64_t *ntasks)
{
int rc = 0;
char *key = lwj_key (h, j, ".ntasks");
flux_future_t *f = NULL;
if (!key || !(f = flux_kvs_lookup (h, 0, key))
|| flux_kvs_lookup_get_unpack (f, "I", ntasks) < 0) {
flux_log_error (h, "extract %s", key);
rc = -1;
}
else
flux_log (h, LOG_DEBUG, "extract %s: %"PRId64"", key, *ntasks);
free (key);
flux_future_destroy (f);
return rc;
}
static int extract_raw_ncores (flux_t *h, int64_t j, int64_t *ncores)
{
int rc = 0;
char *key = lwj_key (h, j, ".ncores");
flux_future_t *f = NULL;
if (!key || !(f = flux_kvs_lookup (h, 0, key))
|| flux_kvs_lookup_get_unpack (f, "I", ncores) < 0) {
flux_log_error (h, "extract %s", key);
rc = -1;
}
else
flux_log (h, LOG_DEBUG, "extract %s: %"PRId64"", key, *ncores);
free (key);
flux_future_destroy (f);
return rc;
}
static int extract_raw_walltime (flux_t *h, int64_t j, int64_t *walltime)
{
int rc = 0;
char *key = lwj_key (h, j, ".walltime");
flux_future_t *f = NULL;
if (!key || !(f = flux_kvs_lookup (h, 0, key))
|| flux_kvs_lookup_get_unpack (f, "I", walltime) < 0) {
flux_log_error (h, "extract %s", key);
rc = -1;
}
else
flux_log (h, LOG_DEBUG, "extract %s: %"PRId64"", key, *walltime);
free (key);
flux_future_destroy (f);
return rc;
}
static int extract_raw_rdl (flux_t *h, int64_t j, char **rdlstr)
{
int rc = 0;
char *key = lwj_key (h, j, ".rdl");
const char *s;
flux_future_t *f = NULL;
if (!key || !(f = flux_kvs_lookup (h, 0, key))
|| flux_kvs_lookup_get_unpack (f, "s", &s) < 0) {
flux_log_error (h, "extract %s", key);
rc = -1;
}
else {
*rdlstr = xstrdup (s);
flux_log (h, LOG_DEBUG, "rdl under %s extracted", key);
}
free (key);
flux_future_destroy (f);
return rc;
}
static int extract_raw_state (flux_t *h, int64_t j, int64_t *s)
{
int rc = 0;
char *key = lwj_key (h, j, ".state");
const char *state;
flux_future_t *f = NULL;
if (!key || !(f = flux_kvs_lookup (h, 0, key))
|| flux_kvs_lookup_get_unpack (f, "s", &state) < 0) {
flux_log_error (h, "extract %s", key);
rc = -1;
}
else {
*s = jsc_job_state2num (state);
flux_log (h, LOG_DEBUG, "extract %s: %s", key, state);
}
free (key);
flux_future_destroy (f);
return rc;
}
static int extract_raw_pdesc (flux_t *h, int64_t j, int64_t i, json_object **o)
{
flux_future_t *f = NULL;
const char *json_str;
char *key = lwj_key (h, j, ".%ju.procdesc", i);
int rc = -1;
if (!key || !(f = flux_kvs_lookup (h, 0, key))
|| flux_kvs_lookup_get (f, &json_str) < 0
|| !(*o = Jfromstr (json_str))) {
flux_log_error (h, "extract %s", key);
goto done;
}
rc = 0;
done:
flux_future_destroy (f);
free (key);
return rc;
}
static json_object *build_parray_elem (int64_t pid, int64_t eix, int64_t hix)
{
json_object *po = Jnew ();
Jadd_int64 (po, JSC_PDESC_RANK_PDARRAY_PID, pid);
Jadd_int64 (po, JSC_PDESC_RANK_PDARRAY_EINDX, eix);
Jadd_int64 (po, JSC_PDESC_RANK_PDARRAY_HINDX, hix);
return po;
}
static void add_pdescs_to_jcb (json_object **hns, json_object **ens,
json_object **pa, json_object *jcb)
{
json_object_object_add (jcb, JSC_PDESC_HOSTNAMES, *hns);
json_object_object_add (jcb, JSC_PDESC_EXECS, *ens);
json_object_object_add (jcb, JSC_PDESC_PDARRAY, *pa);
/* Because the above transfer ownership, assign NULL should be ok */
*hns = NULL;
*ens = NULL;
*pa = NULL;
}
static int extract_raw_pdescs (flux_t *h, int64_t j, int64_t n, json_object *jcb)
{
int rc = -1;
int64_t i = 0;
char *hnm;
const char *cmd = NULL;
zhash_t *eh = NULL; /* hash holding a set of unique exec_names */
zhash_t *hh = NULL; /* hash holding a set of unique host_names */
json_object *o = NULL;
json_object *po = NULL;
json_object *pa = Jnew_ar ();
json_object *hns = Jnew_ar ();
json_object *ens = Jnew_ar ();
if (!(eh = zhash_new ()) || !(hh = zhash_new ()))
oom ();
for (i=0; i < (int) n; i++) {
int64_t eix = 0, hix = 0;
int64_t pid = 0, nid = 0;
if (extract_raw_pdesc (h, j, i, &o) != 0)
goto done;
if (!fetch_rank_pdesc (o, &pid, &nid, &cmd))
goto done;
eix = build_name_array (eh, cmd, ens);
/* FIXME: we need a hostname service */
hnm = xasprintf ("%"PRId64, nid);
hix = build_name_array (hh, hnm, hns);
po = build_parray_elem (pid, eix, hix);
json_object_array_add (pa, po);
po = NULL;
Jput (o);
o = NULL;
free (hnm);
}
add_pdescs_to_jcb (&hns, &ens, &pa, jcb);
rc = 0;
done:
if (o) Jput (o);
if (po) Jput (po);
if (pa) Jput (pa);
if (hns) Jput (hns);
if (ens) Jput (ens);
zhash_destroy (&eh);
zhash_destroy (&hh);
return rc;
}
static int extract_raw_rdl_alloc (flux_t *h, int64_t j, json_object *jcb)
{
int i;
json_object *ra = Jnew_ar ();
bool processing = true;
for (i=0; processing; ++i) {
char *key = lwj_key (h, j, ".rank.%d.cores", i);
flux_future_t *f = NULL;
int64_t cores = 0;
if (!key || !(f = flux_kvs_lookup (h, 0, key))
|| flux_kvs_lookup_get_unpack (f, "I", &cores) < 0) {
if (errno != EINVAL)
flux_log_error (h, "extract %s", key);
processing = false;
} else {
json_object *elem = Jnew ();
json_object *o = Jnew ();
Jadd_int64 (o, JSC_RDL_ALLOC_CONTAINED_NCORES, cores);
json_object_object_add (elem, JSC_RDL_ALLOC_CONTAINED, o);
json_object_array_add (ra, elem);
}
free (key);
flux_future_destroy (f);
}
json_object_object_add (jcb, JSC_RDL_ALLOC, ra);
return 0;
}
static int query_jobid (flux_t *h, int64_t j, json_object **jcb)
{
int rc = 0;
if ( ( rc = jobid_exist (h, j)) != 0)
*jcb = NULL;
else {
*jcb = Jnew ();
Jadd_int64 (*jcb, JSC_JOBID, j);
}
return rc;
}
static int query_state_pair (flux_t *h, int64_t j, json_object **jcb)
{
json_object *o = NULL;
int64_t st = (int64_t)J_FOR_RENT;;
if (extract_raw_state (h, j, &st) < 0) return -1;
*jcb = Jnew ();
o = Jnew ();
/* Old state is unavailable through the query.
* One should use notification service instead.
*/
Jadd_int64 (o, JSC_STATE_PAIR_OSTATE, st);
Jadd_int64 (o, JSC_STATE_PAIR_NSTATE, st);
json_object_object_add (*jcb, JSC_STATE_PAIR, o);
return 0;
}
static int query_rdesc (flux_t *h, int64_t j, json_object **jcb)
{
json_object *o = NULL;
int64_t nnodes = -1;
int64_t ntasks = -1;
int64_t ncores = -1;
int64_t walltime = -1;
if (extract_raw_nnodes (h, j, &nnodes) < 0) return -1;
if (extract_raw_ntasks (h, j, &ntasks) < 0) return -1;
if (extract_raw_ncores (h, j, &ncores) < 0) return -1;
if (extract_raw_walltime (h, j, &walltime) < 0) return -1;
*jcb = Jnew ();
o = Jnew ();
Jadd_int64 (o, JSC_RDESC_NNODES, nnodes);
Jadd_int64 (o, JSC_RDESC_NTASKS, ntasks);
Jadd_int64 (o, JSC_RDESC_NCORES, ncores);
Jadd_int64 (o, JSC_RDESC_WALLTIME, walltime);
json_object_object_add (*jcb, JSC_RDESC, o);
return 0;
}
static int query_rdl (flux_t *h, int64_t j, json_object **jcb)
{
char *rdlstr = NULL;
if (extract_raw_rdl (h, j, &rdlstr) < 0) return -1;
*jcb = Jnew ();
Jadd_str (*jcb, JSC_RDL, (const char *)rdlstr);
/* Note: seems there is no mechanism to transfer ownership
* of this string to jcb */
if (rdlstr)
free (rdlstr);
return 0;
}
static int query_rdl_alloc (flux_t *h, int64_t j, json_object **jcb)
{
*jcb = Jnew ();
return extract_raw_rdl_alloc (h, j, *jcb);
}
static int query_pdesc (flux_t *h, int64_t j, json_object **jcb)
{
int64_t ntasks = 0;
if (extract_raw_ntasks (h, j, &ntasks) < 0) return -1;
*jcb = Jnew ();
Jadd_int64 (*jcb, JSC_PDESC_SIZE, ntasks);
return extract_raw_pdescs (h, j, ntasks, *jcb);
}
static int send_state_event (flux_t *h, job_state_t st, int64_t j)
{
flux_msg_t *msg;
char *json = NULL;
char *topic = NULL;
int rc = -1;
if (asprintf (&topic, "jsc.state.%s", jsc_job_num2state (st)) < 0) {
errno = ENOMEM;
flux_log_error (h, "create state change event: %s",
jsc_job_num2state (st));
goto done;
}
if ((msg = flux_event_pack (topic, "{ s:I }", "jobid", j)) == NULL) {
flux_log_error (h, "flux_event_pack");
goto done;
}
if (flux_send (h, msg, 0) < 0)
flux_log_error (h, "flux_send event");
flux_msg_destroy (msg);
rc = 0;
done:
free (topic);
free (json);
return rc;
}
static int update_state (flux_t *h, int64_t j, json_object *o)
{
int rc = -1;
int64_t st = 0;
char *key = NULL;
flux_kvs_txn_t *txn = NULL;
flux_future_t *f = NULL;
if (!Jget_int64 (o, JSC_STATE_PAIR_NSTATE, &st))
goto done;
if ((st >= J_FOR_RENT) || (st < J_NULL))
goto done;
if (!(key = lwj_key (h, j, ".state")))
goto done;
if (!(txn = flux_kvs_txn_create ())) {
flux_log_error (h, "txn_create");
goto done;
}
if (flux_kvs_txn_pack (txn, 0, key, "s",
jsc_job_num2state ((job_state_t)st)) < 0) {
flux_log_error (h, "update %s", key);
goto done;
}
if (!(f = flux_kvs_commit (h, 0, txn)) || flux_future_get (f, NULL) < 0) {
flux_log_error (h, "commit %s", key);
goto done;
}
flux_log (h, LOG_DEBUG, "job (%"PRId64") assigned new state: %s", j,
jsc_job_num2state ((job_state_t)st));
rc = 0;
if (send_state_event (h, st, j) < 0)
flux_log_error (h, "send state event");
done:
flux_future_destroy (f);
flux_kvs_txn_destroy (txn);
free (key);
return rc;
}
static int update_rdesc (flux_t *h, int64_t j, json_object *o)
{
int rc = -1;
int64_t nnodes = 0;
int64_t ntasks = 0;
int64_t ncores = 0;
int64_t walltime = 0;
char *key1 = NULL;
char *key2 = NULL;
char *key3 = NULL;
char *key4 = NULL;
flux_kvs_txn_t *txn = NULL;
flux_future_t *f = NULL;
if (!Jget_int64 (o, JSC_RDESC_NNODES, &nnodes))
goto done;
if (!Jget_int64 (o, JSC_RDESC_NTASKS, &ntasks))
goto done;
if (!Jget_int64 (o, JSC_RDESC_NCORES, &ncores))
goto done;
if (!Jget_int64 (o, JSC_RDESC_WALLTIME, &walltime))
goto done;
if ((nnodes < 0) || (ntasks < 0) || (ncores < 0) || (walltime < 0))
goto done;
key1 = lwj_key (h, j, ".nnodes");
key2 = lwj_key (h, j, ".ntasks");
key3 = lwj_key (h, j, ".walltime");
key4 = lwj_key (h, j, ".ncores");
if (!key1 || !key2 || !key3 || !key4)
goto done;
if (!(txn = flux_kvs_txn_create ())) {
flux_log_error (h, "txn_create");
goto done;
}
if (flux_kvs_txn_pack (txn, 0, key1, "I", nnodes) < 0) {
flux_log_error (h, "update %s", key1);
goto done;
}
if (flux_kvs_txn_pack (txn, 0, key2, "I", ntasks) < 0) {
flux_log_error (h, "update %s", key2);
goto done;
}
if (flux_kvs_txn_pack (txn, 0, key3, "I", walltime) < 0) {
flux_log_error (h, "update %s", key3);
goto done;
}
if (flux_kvs_txn_pack (txn, 0, key4, "I", ncores) < 0) {
flux_log_error (h, "update %s", key4);
goto done;
}
if (!(f = flux_kvs_commit (h, 0, txn)) || flux_future_get (f, NULL) < 0) {
flux_log_error (h, "commit failed");
goto done;
}
flux_log (h, LOG_DEBUG, "job (%"PRId64") assigned new resources.", j);
rc = 0;
done:
flux_future_destroy (f);
flux_kvs_txn_destroy (txn);
free (key1);
free (key2);
free (key3);
free (key4);
return rc;
}
static int update_rdl (flux_t *h, int64_t j, const char *rs)
{
int rc = -1;
char *key = lwj_key (h, j, ".rdl");
flux_kvs_txn_t *txn = NULL;
flux_future_t *f = NULL;
if (!(txn = flux_kvs_txn_create ())) {
flux_log_error (h, "txn_create");
goto done;
}
if (flux_kvs_txn_pack (txn, 0, key, "s", rs) < 0) {
flux_log_error (h, "update %s", key);
goto done;
}
if (!(f = flux_kvs_commit (h, 0, txn)) || flux_future_get (f, NULL) < 0) {
flux_log_error (h, "commit failed");
goto done;
}
flux_log (h, LOG_DEBUG, "job (%"PRId64") assigned new rdl.", j);
rc = 0;
done:
flux_kvs_txn_destroy (txn);
flux_future_destroy (f);
free (key);
return rc;
}
static int update_hash_1ra (flux_t *h, int64_t j, json_object *o, zhash_t *rtab)
{
int rc = 0;
int64_t ncores = 0;
int64_t rank = 0;
int64_t *curcnt;
char *key;
json_object *c = NULL;
if (!Jget_obj (o, JSC_RDL_ALLOC_CONTAINED, &c)) return -1;
if (!Jget_int64 (c, JSC_RDL_ALLOC_CONTAINING_RANK, &rank)) return -1;
if (!Jget_int64 (c, JSC_RDL_ALLOC_CONTAINED_NCORES, &ncores)) return -1;
key = lwj_key (h, j, ".rank.%ju.cores", rank);
if (!(curcnt = zhash_lookup (rtab, key))) {
curcnt = xzmalloc (sizeof (*curcnt));
*curcnt = ncores;
zhash_insert (rtab, key, curcnt);
zhash_freefn (rtab, key, free);
} else
*curcnt = *curcnt + ncores;
free (key);
return rc;
}
static int update_rdl_alloc (flux_t *h, int64_t j, json_object *o)
{
int i = 0;
int rc = -1;
int size = 0;
json_object *ra_e = NULL;
const char *key = NULL;
zhash_t *rtab = NULL;
int64_t *ncores = NULL;
flux_kvs_txn_t *txn = NULL;
flux_future_t *f = NULL;
if (!(rtab = zhash_new ()))
oom ();
if (!Jget_ar_len (o, &size))
goto done;
for (i=0; i < (int) size; ++i) {
if (!Jget_ar_obj (o, i, &ra_e))
goto done;
/* 'o' represents an array of per-node core count to use.
* However, becasue the same rank can appear multiple times
* in this array in emulation mode, update_hash_1ra is
* used to determine the total core count per rank.
*/
if ( (rc = update_hash_1ra (h, j, ra_e, rtab)) < 0)
goto done;
}
if (!(txn = flux_kvs_txn_create ())) {
flux_log_error (h, "txn_create");
goto done;
}
FOREACH_ZHASH (rtab, key, ncores) {
if ( (rc = flux_kvs_txn_pack (txn, 0, key, "I", *ncores)) < 0) {
flux_log_error (h, "put %s", key);
goto done;
}
}
if (!(f = flux_kvs_commit (h, 0, txn)) || flux_future_get (f, NULL) < 0) {
flux_log (h, LOG_ERR, "update_pdesc commit failed");
goto done;
}
rc = 0;
done:
flux_future_destroy (f);
flux_kvs_txn_destroy (txn);
zhash_destroy (&rtab);
return rc;
}
static int update_1pdesc (flux_t *h, flux_kvs_txn_t *txn,
int r, int64_t j, json_object *o,
json_object *ha, json_object *ea)
{
flux_future_t *f = NULL;
int rc = -1;
json_object *d = NULL;
char *key;
const char *json_str;
const char *hn = NULL, *en = NULL;
int64_t pid = 0, hindx = 0, eindx = 0, hrank = 0;
if (!Jget_int64 (o, JSC_PDESC_RANK_PDARRAY_PID, &pid)) return -1;
if (!Jget_int64 (o, JSC_PDESC_RANK_PDARRAY_HINDX, &hindx)) return -1;
if (!Jget_int64 (o, JSC_PDESC_RANK_PDARRAY_EINDX, &eindx)) return -1;
if (!Jget_ar_str (ha, (int)hindx, &hn)) return -1;
if (!Jget_ar_str (ea, (int)eindx, &en)) return -1;
key = lwj_key (h, j, ".%d.procdesc", r);
if (!key || !(f = flux_kvs_lookup (h, 0, key))
|| flux_kvs_lookup_get (f, &json_str) < 0
|| !(d = Jfromstr (json_str))) {
flux_log_error (h, "extract %s", key);
goto done;
}
Jadd_str (d, "command", en);
Jadd_int64 (d, "pid", pid);
errno = 0;
if ( (hrank = strtoul (hn, NULL, 10)) && errno != 0) {
flux_log (h, LOG_ERR, "invalid hostname %s", hn);
goto done;
}
Jadd_int64 (d, "nodeid", (int64_t)hrank);
if (flux_kvs_txn_put (txn, 0, key, Jtostr (d)) < 0) {
flux_log_error (h, "put %s", key);
goto done;
}
rc = 0;
done:
flux_future_destroy (f);
free (key);
if (d)
Jput (d);
return rc;
}
static int update_pdesc (flux_t *h, int64_t j, json_object *o)
{
int i = 0;
int rc = -1;
int64_t size = 0;
json_object *h_arr = NULL;
json_object *e_arr = NULL;
json_object *pd_arr = NULL;
json_object *pde = NULL;
flux_kvs_txn_t *txn = NULL;
flux_future_t *f = NULL;
if (!Jget_int64 (o, JSC_PDESC_SIZE, &size))
goto done;
if (!Jget_obj (o, JSC_PDESC_PDARRAY, &pd_arr))
goto done;
if (!Jget_obj (o, JSC_PDESC_HOSTNAMES, &h_arr))
goto done;
if (!Jget_obj (o, JSC_PDESC_EXECS, &e_arr))
goto done;
if (!(txn = flux_kvs_txn_create ())) {
flux_log_error (h, "txn_create");
goto done;
}
for (i=0; i < (int) size; ++i) {
if (!Jget_ar_obj (pd_arr, i, &pde))
goto done;
if ( (rc = update_1pdesc (h, txn, i, j, pde, h_arr, e_arr)) < 0)
goto done;
}
if (!(f = flux_kvs_commit (h, 0, txn)) || flux_future_get (f, NULL) < 0) {
flux_log (h, LOG_ERR, "update_pdesc commit failed");
goto done;
}
rc = 0;
done:
flux_kvs_txn_destroy (txn);
flux_future_destroy (f);
return rc;
}
static json_object *get_update_jcb (flux_t *h, int64_t j, const char *val)
{
json_object *o = NULL;
json_object *ss = NULL;
jscctx_t *ctx = getctx (h);
int64_t ostate = (int64_t) J_FOR_RENT;
int64_t nstate = (int64_t) J_FOR_RENT;
nstate = jsc_job_state2num (val);
if ( (ostate = fetch_and_update_state (ctx->active_jobs, j, nstate)) < 0) {
flux_log (h, LOG_INFO, "%"PRId64"'s old state unavailable", j);
ostate = nstate;
}