Merge pull request #20109 from keszybz/timestamp-macros
Add macros that define scratch buffer internally for timestamp/timespan formatting
This commit is contained in:
+2
-5
@@ -120,13 +120,10 @@ int efi_get_variable(
|
||||
n = st.st_size - 4;
|
||||
|
||||
if (DEBUG_LOGGING) {
|
||||
char ts[FORMAT_TIMESPAN_MAX];
|
||||
usec_t end;
|
||||
|
||||
end = now(CLOCK_MONOTONIC);
|
||||
usec_t end = now(CLOCK_MONOTONIC);
|
||||
if (end > begin + EFI_RETRY_DELAY)
|
||||
log_debug("Detected slow EFI variable read access on %s: %s",
|
||||
variable, format_timespan(ts, sizeof(ts), end - begin, 1));
|
||||
variable, FORMAT_TIMESPAN(end - begin, 1));
|
||||
}
|
||||
|
||||
/* Note that efivarfs interestingly doesn't require ftruncate() to update an existing EFI variable
|
||||
|
||||
@@ -23,7 +23,9 @@ struct hw_addr_data {
|
||||
#define HW_ADDR_TO_STRING_MAX (3*HW_ADDR_MAX_SIZE)
|
||||
char* hw_addr_to_string(const struct hw_addr_data *addr, char buffer[HW_ADDR_TO_STRING_MAX]);
|
||||
|
||||
/* Use only as function argument, never stand-alone! */
|
||||
/* Note: the lifetime of the compound literal is the immediately surrounding block,
|
||||
* see C11 §6.5.2.5, and
|
||||
* https://stackoverflow.com/questions/34880638/compound-literal-lifetime-and-if-blocks */
|
||||
#define HW_ADDR_TO_STR(hw_addr) hw_addr_to_string((hw_addr), (char[HW_ADDR_TO_STRING_MAX]){})
|
||||
|
||||
#define HW_ADDR_NULL ((const struct hw_addr_data){})
|
||||
|
||||
@@ -74,16 +74,17 @@ typedef enum {
|
||||
|
||||
#define FORMAT_BYTES_MAX 16U
|
||||
|
||||
char *format_bytes_full(char *buf, size_t l, uint64_t t, FormatBytesFlag flag);
|
||||
char *format_bytes_full(char *buf, size_t l, uint64_t t, FormatBytesFlag flag) _warn_unused_result_;
|
||||
|
||||
_warn_unused_result_
|
||||
static inline char *format_bytes(char *buf, size_t l, uint64_t t) {
|
||||
return format_bytes_full(buf, l, t, FORMAT_BYTES_USE_IEC | FORMAT_BYTES_BELOW_POINT | FORMAT_BYTES_TRAILING_B);
|
||||
}
|
||||
|
||||
static inline char *format_bytes_cgroup_protection(char *buf, size_t l, uint64_t t) {
|
||||
if (t == CGROUP_LIMIT_MAX) {
|
||||
(void) snprintf(buf, l, "%s", "infinity");
|
||||
return buf;
|
||||
}
|
||||
return format_bytes(buf, l, t);
|
||||
}
|
||||
/* Note: the lifetime of the compound literal is the immediately surrounding block,
|
||||
* see C11 §6.5.2.5, and
|
||||
* https://stackoverflow.com/questions/34880638/compound-literal-lifetime-and-if-blocks */
|
||||
#define FORMAT_BYTES(t) format_bytes((char[FORMAT_BYTES_MAX]){}, FORMAT_BYTES_MAX, t)
|
||||
#define FORMAT_BYTES_FULL(t, flag) format_bytes_full((char[FORMAT_BYTES_MAX]){}, FORMAT_BYTES_MAX, t, flag)
|
||||
|
||||
#define FORMAT_BYTES_CGROUP_PROTECTION(t) (t == CGROUP_LIMIT_MAX ? "infinity" : FORMAT_BYTES(t))
|
||||
|
||||
+6
-5
@@ -30,6 +30,7 @@
|
||||
#define _weakref_(x) __attribute__((__weakref__(#x)))
|
||||
#define _alignas_(x) __attribute__((__aligned__(__alignof(x))))
|
||||
#define _alignptr_ __attribute__((__aligned__(sizeof(void*))))
|
||||
#define _warn_unused_result_ __attribute__((__warn_unused_result__))
|
||||
#if __GNUC__ >= 7
|
||||
#define _fallthrough_ __attribute__((__fallthrough__))
|
||||
#else
|
||||
@@ -213,7 +214,7 @@ static inline size_t GREEDY_ALLOC_ROUND_UP(size_t l) {
|
||||
* Contrary to strlen(), this is a constant expression.
|
||||
* @x: a string literal.
|
||||
*/
|
||||
#define STRLEN(x) (sizeof(""x"") - 1)
|
||||
#define STRLEN(x) ((unsigned) sizeof(""x"") - 1)
|
||||
|
||||
/*
|
||||
* container_of - cast a member of a structure out to the containing structure
|
||||
@@ -341,10 +342,10 @@ static inline int __coverity_check_and_return__(int condition) {
|
||||
* negative '-' prefix (hence works correctly on signed
|
||||
* types). Includes space for the trailing NUL. */
|
||||
#define DECIMAL_STR_MAX(type) \
|
||||
(2+(sizeof(type) <= 1 ? 3 : \
|
||||
sizeof(type) <= 2 ? 5 : \
|
||||
sizeof(type) <= 4 ? 10 : \
|
||||
sizeof(type) <= 8 ? 20 : sizeof(int[-2*(sizeof(type) > 8)])))
|
||||
(2U+(sizeof(type) <= 1 ? 3U : \
|
||||
sizeof(type) <= 2 ? 5U : \
|
||||
sizeof(type) <= 4 ? 10U : \
|
||||
sizeof(type) <= 8 ? 20U : (unsigned) sizeof(int[-2*(sizeof(type) > 8)])))
|
||||
|
||||
#define DECIMAL_STR_WIDTH(x) \
|
||||
({ \
|
||||
|
||||
@@ -300,26 +300,26 @@ int rlimit_parse(int resource, const char *val, struct rlimit *ret) {
|
||||
}
|
||||
|
||||
int rlimit_format(const struct rlimit *rl, char **ret) {
|
||||
char *s = NULL;
|
||||
_cleanup_free_ char *s = NULL;
|
||||
int r;
|
||||
|
||||
assert(rl);
|
||||
assert(ret);
|
||||
|
||||
if (rl->rlim_cur >= RLIM_INFINITY && rl->rlim_max >= RLIM_INFINITY)
|
||||
s = strdup("infinity");
|
||||
r = free_and_strdup(&s, "infinity");
|
||||
else if (rl->rlim_cur >= RLIM_INFINITY)
|
||||
(void) asprintf(&s, "infinity:" RLIM_FMT, rl->rlim_max);
|
||||
r = asprintf(&s, "infinity:" RLIM_FMT, rl->rlim_max);
|
||||
else if (rl->rlim_max >= RLIM_INFINITY)
|
||||
(void) asprintf(&s, RLIM_FMT ":infinity", rl->rlim_cur);
|
||||
r = asprintf(&s, RLIM_FMT ":infinity", rl->rlim_cur);
|
||||
else if (rl->rlim_cur == rl->rlim_max)
|
||||
(void) asprintf(&s, RLIM_FMT, rl->rlim_cur);
|
||||
r = asprintf(&s, RLIM_FMT, rl->rlim_cur);
|
||||
else
|
||||
(void) asprintf(&s, RLIM_FMT ":" RLIM_FMT, rl->rlim_cur, rl->rlim_max);
|
||||
|
||||
if (!s)
|
||||
r = asprintf(&s, RLIM_FMT ":" RLIM_FMT, rl->rlim_cur, rl->rlim_max);
|
||||
if (r < 0)
|
||||
return -ENOMEM;
|
||||
|
||||
*ret = s;
|
||||
*ret = TAKE_PTR(s);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
+25
-16
@@ -111,20 +111,31 @@ usec_t triple_timestamp_by_clock(triple_timestamp *ts, clockid_t clock);
|
||||
|
||||
usec_t timespec_load(const struct timespec *ts) _pure_;
|
||||
nsec_t timespec_load_nsec(const struct timespec *ts) _pure_;
|
||||
struct timespec *timespec_store(struct timespec *ts, usec_t u);
|
||||
struct timespec *timespec_store_nsec(struct timespec *ts, nsec_t n);
|
||||
struct timespec* timespec_store(struct timespec *ts, usec_t u);
|
||||
struct timespec* timespec_store_nsec(struct timespec *ts, nsec_t n);
|
||||
|
||||
usec_t timeval_load(const struct timeval *tv) _pure_;
|
||||
struct timeval *timeval_store(struct timeval *tv, usec_t u);
|
||||
struct timeval* timeval_store(struct timeval *tv, usec_t u);
|
||||
|
||||
char *format_timestamp_style(char *buf, size_t l, usec_t t, TimestampStyle style);
|
||||
char *format_timestamp_relative(char *buf, size_t l, usec_t t);
|
||||
char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy);
|
||||
char* format_timestamp_style(char *buf, size_t l, usec_t t, TimestampStyle style) _warn_unused_result_;
|
||||
char* format_timestamp_relative(char *buf, size_t l, usec_t t) _warn_unused_result_;
|
||||
char* format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) _warn_unused_result_;
|
||||
|
||||
static inline char *format_timestamp(char *buf, size_t l, usec_t t) {
|
||||
_warn_unused_result_
|
||||
static inline char* format_timestamp(char *buf, size_t l, usec_t t) {
|
||||
return format_timestamp_style(buf, l, t, TIMESTAMP_PRETTY);
|
||||
}
|
||||
|
||||
/* Note: the lifetime of the compound literal is the immediately surrounding block,
|
||||
* see C11 §6.5.2.5, and
|
||||
* https://stackoverflow.com/questions/34880638/compound-literal-lifetime-and-if-blocks */
|
||||
#define FORMAT_TIMESTAMP(t) format_timestamp((char[FORMAT_TIMESTAMP_MAX]){}, FORMAT_TIMESTAMP_MAX, t)
|
||||
#define FORMAT_TIMESTAMP_RELATIVE(t) \
|
||||
format_timestamp_relative((char[FORMAT_TIMESTAMP_RELATIVE_MAX]){}, FORMAT_TIMESTAMP_RELATIVE_MAX, t)
|
||||
#define FORMAT_TIMESPAN(t, accuracy) format_timespan((char[FORMAT_TIMESPAN_MAX]){}, FORMAT_TIMESPAN_MAX, t, accuracy)
|
||||
#define FORMAT_TIMESTAMP_STYLE(t, style) \
|
||||
format_timestamp_style((char[FORMAT_TIMESTAMP_MAX]){}, FORMAT_TIMESTAMP_MAX, t, style)
|
||||
|
||||
int parse_timestamp(const char *t, usec_t *usec);
|
||||
|
||||
int parse_sec(const char *t, usec_t *usec);
|
||||
@@ -156,9 +167,8 @@ usec_t jiffies_to_usec(uint32_t jiffies);
|
||||
bool in_utc_timezone(void);
|
||||
|
||||
static inline usec_t usec_add(usec_t a, usec_t b) {
|
||||
|
||||
/* Adds two time values, and makes sure USEC_INFINITY as input results as USEC_INFINITY in output, and doesn't
|
||||
* overflow. */
|
||||
/* Adds two time values, and makes sure USEC_INFINITY as input results as USEC_INFINITY in output,
|
||||
* and doesn't overflow. */
|
||||
|
||||
if (a > USEC_INFINITY - b) /* overflow check */
|
||||
return USEC_INFINITY;
|
||||
@@ -167,7 +177,6 @@ static inline usec_t usec_add(usec_t a, usec_t b) {
|
||||
}
|
||||
|
||||
static inline usec_t usec_sub_unsigned(usec_t timestamp, usec_t delta) {
|
||||
|
||||
if (timestamp == USEC_INFINITY) /* Make sure infinity doesn't degrade */
|
||||
return USEC_INFINITY;
|
||||
if (timestamp < delta)
|
||||
@@ -184,14 +193,14 @@ static inline usec_t usec_sub_signed(usec_t timestamp, int64_t delta) {
|
||||
}
|
||||
|
||||
#if SIZEOF_TIME_T == 8
|
||||
/* The last second we can format is 31. Dec 9999, 1s before midnight, because otherwise we'd enter 5 digit year
|
||||
* territory. However, since we want to stay away from this in all timezones we take one day off. */
|
||||
#define USEC_TIMESTAMP_FORMATTABLE_MAX ((usec_t) 253402214399000000)
|
||||
/* The last second we can format is 31. Dec 9999, 1s before midnight, because otherwise we'd enter 5 digit
|
||||
* year territory. However, since we want to stay away from this in all timezones we take one day off. */
|
||||
# define USEC_TIMESTAMP_FORMATTABLE_MAX ((usec_t) 253402214399000000)
|
||||
#elif SIZEOF_TIME_T == 4
|
||||
/* With a 32bit time_t we can't go beyond 2038... */
|
||||
#define USEC_TIMESTAMP_FORMATTABLE_MAX ((usec_t) 2147483647000000)
|
||||
# define USEC_TIMESTAMP_FORMATTABLE_MAX ((usec_t) 2147483647000000)
|
||||
#else
|
||||
#error "Yuck, time_t is neither 4 nor 8 bytes wide?"
|
||||
# error "Yuck, time_t is neither 4 nor 8 bytes wide?"
|
||||
#endif
|
||||
|
||||
int time_change_fd(void);
|
||||
|
||||
Reference in New Issue
Block a user