Skip to content

Commit 2c617e4

Browse files
committed
libjob: add convenience idf58() function
Problem: The F58 representation of a Flux jobid is now the de facto default representation of a jobid, but converting the flux_jobid_t representation to this format for logging and other purposes is a bit awkward. This makes it less likely for in-tree users to represent a job in this format, and when they do, this requires a local buffer, a call to flux_job_id_encode(3), etc. Add a very simple, header-only conversion function to libjob for this purpose const char *idf58 (flux_jobid_t id); This function uses a thread-local buffer to avoid requiring callers to allocate their own on the stack. This makes it much easier (and thus much more likely) to log jobids in F58, since this function can just be placed around the jobid, instead of requiring a local buffer and previous function call the encode the id.
1 parent 2e502e8 commit 2c617e4

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/common/libjob/Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ libjob_la_SOURCES = \
3939
jj.c \
4040
jj.h \
4141
unwrap.c \
42-
unwrap.h
42+
unwrap.h \
43+
idf58.h
4344

4445
TESTS = \
4546
test_job.t \

src/common/libjob/idf58.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/************************************************************\
2+
* Copyright 2023 Lawrence Livermore National Security, LLC
3+
* (c.f. AUTHORS, NOTICE.LLNS, COPYING)
4+
*
5+
* This file is part of the Flux resource manager framework.
6+
* For details, see https://github.com/flux-framework.
7+
*
8+
* SPDX-License-Identifier: LGPL-3.0
9+
\************************************************************/
10+
11+
#ifndef _IDF58_H
12+
#define _IDF58_H
13+
14+
#include <flux/core.h>
15+
16+
/* Convenience function to convert a flux_jobid_t to F58 encoding
17+
* If the encode fails (unlikely), then the decimal encoding is returned.
18+
*/
19+
static inline const char *idf58 (flux_jobid_t id)
20+
{
21+
static __thread char buf[21];
22+
if (flux_job_id_encode (id, "f58", buf, sizeof (buf)) < 0) {
23+
/* 64bit integer is guaranteed to fit in 21 bytes
24+
* floor(log(2^64-1)/log(1)) + 1 = 20
25+
*/
26+
(void) sprintf (buf, "%ju", (uintmax_t) id);
27+
}
28+
return buf;
29+
}
30+
31+
#endif /* !_IDF58_H */
32+
33+
/*
34+
* vi:tabstop=4 shiftwidth=4 expandtab
35+
*/

0 commit comments

Comments
 (0)