Skip to content

Commit 3b7c5e8

Browse files
committed
tools/thermal: Add util library
The next changes will provide a couple of tools using some common functions provided by this library. It provides basic wrappers for: - mainloop - logging - timestamp Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org> Tested-by: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com> Link: https://lore.kernel.org/r/20220420160933.347088-3-daniel.lezcano@linaro.org
1 parent 47c4b0d commit 3b7c5e8

10 files changed

Lines changed: 478 additions & 0 deletions

File tree

tools/thermal/lib/Build

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
libthermal_tools-y += mainloop.o
2+
libthermal_tools-y += log.o
3+
libthermal_tools-y += uptimeofday.o

tools/thermal/lib/Makefile

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2+
# Most of this file is copied from tools/lib/perf/Makefile
3+
4+
LIBTHERMAL_TOOLS_VERSION = 0
5+
LIBTHERMAL_TOOLS_PATCHLEVEL = 0
6+
LIBTHERMAL_TOOLS_EXTRAVERSION = 1
7+
8+
MAKEFLAGS += --no-print-directory
9+
10+
ifeq ($(srctree),)
11+
srctree := $(patsubst %/,%,$(dir $(CURDIR)))
12+
srctree := $(patsubst %/,%,$(dir $(srctree)))
13+
srctree := $(patsubst %/,%,$(dir $(srctree)))
14+
# $(info Determined 'srctree' to be $(srctree))
15+
endif
16+
17+
INSTALL = install
18+
19+
# Use DESTDIR for installing into a different root directory.
20+
# This is useful for building a package. The program will be
21+
# installed in this directory as if it was the root directory.
22+
# Then the build tool can move it later.
23+
DESTDIR ?=
24+
DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))'
25+
26+
include $(srctree)/tools/scripts/Makefile.include
27+
include $(srctree)/tools/scripts/Makefile.arch
28+
29+
ifeq ($(LP64), 1)
30+
libdir_relative = lib64
31+
else
32+
libdir_relative = lib
33+
endif
34+
35+
prefix ?=
36+
libdir = $(prefix)/$(libdir_relative)
37+
38+
# Shell quotes
39+
libdir_SQ = $(subst ','\'',$(libdir))
40+
libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
41+
42+
ifeq ("$(origin V)", "command line")
43+
VERBOSE = $(V)
44+
endif
45+
ifndef VERBOSE
46+
VERBOSE = 0
47+
endif
48+
49+
ifeq ($(VERBOSE),1)
50+
Q =
51+
else
52+
Q = @
53+
endif
54+
55+
# Set compile option CFLAGS
56+
ifdef EXTRA_CFLAGS
57+
CFLAGS := $(EXTRA_CFLAGS)
58+
else
59+
CFLAGS := -g -Wall
60+
endif
61+
62+
INCLUDES = \
63+
-I/usr/include/libnl3 \
64+
-I$(srctree)/tools/lib/thermal/include \
65+
-I$(srctree)/tools/lib/ \
66+
-I$(srctree)/tools/include \
67+
-I$(srctree)/tools/arch/$(SRCARCH)/include/ \
68+
-I$(srctree)/tools/arch/$(SRCARCH)/include/uapi \
69+
-I$(srctree)/tools/include/uapi
70+
71+
# Append required CFLAGS
72+
override CFLAGS += $(EXTRA_WARNINGS)
73+
override CFLAGS += -Werror -Wall
74+
override CFLAGS += -fPIC
75+
override CFLAGS += $(INCLUDES)
76+
override CFGLAS += -Wl,-L.
77+
override CFGLAS += -Wl,-lthermal
78+
79+
all:
80+
81+
export srctree OUTPUT CC LD CFLAGS V
82+
export DESTDIR DESTDIR_SQ
83+
84+
include $(srctree)/tools/build/Makefile.include
85+
86+
PATCHLEVEL = $(LIBTHERMAL_TOOLS_PATCHLEVEL)
87+
EXTRAVERSION = $(LIBTHERMAL_TOOLS_EXTRAVERSION)
88+
VERSION = $(LIBTHERMAL_TOOLS_VERSION).$(LIBTHERMAL_TOOLS_PATCHLEVEL).$(LIBTHERMAL_TOOLS_EXTRAVERSION)
89+
90+
LIBTHERMAL_TOOLS_SO := $(OUTPUT)libthermal_tools.so.$(VERSION)
91+
LIBTHERMAL_TOOLS_A := $(OUTPUT)libthermal_tools.a
92+
LIBTHERMAL_TOOLS_IN := $(OUTPUT)libthermal_tools-in.o
93+
LIBTHERMAL_TOOLS_PC := $(OUTPUT)libthermal_tools.pc
94+
95+
LIBTHERMAL_TOOLS_ALL := $(LIBTHERMAL_TOOLS_A) $(OUTPUT)libthermal_tools.so*
96+
97+
$(LIBTHERMAL_TOOLS_IN): FORCE
98+
$(Q)$(MAKE) $(build)=libthermal_tools
99+
100+
$(LIBTHERMAL_TOOLS_A): $(LIBTHERMAL_TOOLS_IN)
101+
$(QUIET_AR)$(RM) $@ && $(AR) rcs $@ $(LIBTHERMAL_TOOLS_IN)
102+
103+
$(LIBTHERMAL_TOOLS_SO): $(LIBTHERMAL_TOOLS_IN)
104+
$(QUIET_LINK)$(CC) --shared -Wl,-soname,libthermal_tools.so $^ -o $@
105+
@ln -sf $(@F) $(OUTPUT)libthermal_tools.so
106+
@ln -sf $(@F) $(OUTPUT)libthermal_tools.so.$(LIBTHERMAL_TOOLS_VERSION)
107+
108+
109+
libs: $(LIBTHERMAL_TOOLS_A) $(LIBTHERMAL_TOOLS_SO) $(LIBTHERMAL_TOOLS_PC)
110+
111+
all: fixdep
112+
$(Q)$(MAKE) libs
113+
114+
clean:
115+
$(call QUIET_CLEAN, libthermal_tools) $(RM) $(LIBTHERMAL_TOOLS_A) \
116+
*.o *~ *.a *.so *.so.$(VERSION) *.so.$(LIBTHERMAL_TOOLS_VERSION) .*.d .*.cmd LIBTHERMAL_TOOLS-CFLAGS $(LIBTHERMAL_TOOLS_PC)
117+
118+
$(LIBTHERMAL_TOOLS_PC):
119+
$(QUIET_GEN)sed -e "s|@PREFIX@|$(prefix)|" \
120+
-e "s|@LIBDIR@|$(libdir_SQ)|" \
121+
-e "s|@VERSION@|$(VERSION)|" \
122+
< libthermal_tools.pc.template > $@
123+
124+
define do_install_mkdir
125+
if [ ! -d '$(DESTDIR_SQ)$1' ]; then \
126+
$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$1'; \
127+
fi
128+
endef
129+
130+
define do_install
131+
if [ ! -d '$(DESTDIR_SQ)$2' ]; then \
132+
$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2'; \
133+
fi; \
134+
$(INSTALL) $1 $(if $3,-m $3,) '$(DESTDIR_SQ)$2'
135+
endef
136+
137+
install_lib: libs
138+
$(call QUIET_INSTALL, $(LIBTHERMAL_TOOLS_ALL)) \
139+
$(call do_install_mkdir,$(libdir_SQ)); \
140+
cp -fpR $(LIBTHERMAL_TOOLS_ALL) $(DESTDIR)$(libdir_SQ)
141+
142+
install_headers:
143+
$(call QUIET_INSTALL, headers) \
144+
$(call do_install,include/thermal.h,$(prefix)/include/thermal,644); \
145+
146+
install_pkgconfig: $(LIBTHERMAL_TOOLS_PC)
147+
$(call QUIET_INSTALL, $(LIBTHERMAL_TOOLS_PC)) \
148+
$(call do_install,$(LIBTHERMAL_TOOLS_PC),$(libdir_SQ)/pkgconfig,644)
149+
150+
install_doc:
151+
$(Q)$(MAKE) -C Documentation install-man install-html install-examples
152+
153+
#install: install_lib install_headers install_pkgconfig install_doc
154+
install: install_lib install_headers install_pkgconfig
155+
156+
FORCE:
157+
158+
.PHONY: all install clean FORCE
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2+
3+
prefix=@PREFIX@
4+
libdir=@LIBDIR@
5+
includedir=${prefix}/include
6+
7+
Name: libthermal
8+
Description: thermal library
9+
Requires: libnl-3.0 libnl-genl-3.0
10+
Version: @VERSION@
11+
Libs: -L${libdir} -lnl-genl-3 -lnl-3
12+
Cflags: -I${includedir} -I{include}/libnl3

tools/thermal/lib/log.c

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// SPDX-License-Identifier: LGPL-2.1+
2+
// Copyright (C) 2022, Linaro Ltd - Daniel Lezcano <daniel.lezcano@linaro.org>
3+
#include <stdarg.h>
4+
#include <stdio.h>
5+
#include <string.h>
6+
#include <syslog.h>
7+
#include "log.h"
8+
9+
static const char *__ident = "unknown";
10+
static int __options;
11+
12+
static const char * const loglvl[] = {
13+
[LOG_DEBUG] = "DEBUG",
14+
[LOG_INFO] = "INFO",
15+
[LOG_NOTICE] = "NOTICE",
16+
[LOG_WARNING] = "WARN",
17+
[LOG_ERR] = "ERROR",
18+
[LOG_CRIT] = "CRITICAL",
19+
[LOG_ALERT] = "ALERT",
20+
[LOG_EMERG] = "EMERG",
21+
};
22+
23+
int log_str2level(const char *lvl)
24+
{
25+
int i;
26+
27+
for (i = 0; i < sizeof(loglvl) / sizeof(loglvl[LOG_DEBUG]); i++)
28+
if (!strcmp(lvl, loglvl[i]))
29+
return i;
30+
31+
return LOG_DEBUG;
32+
}
33+
34+
extern void logit(int level, const char *format, ...)
35+
{
36+
va_list args;
37+
38+
va_start(args, format);
39+
40+
if (__options & TO_SYSLOG)
41+
vsyslog(level, format, args);
42+
43+
if (__options & TO_STDERR)
44+
vfprintf(stderr, format, args);
45+
46+
if (__options & TO_STDOUT)
47+
vfprintf(stdout, format, args);
48+
49+
va_end(args);
50+
}
51+
52+
int log_init(int level, const char *ident, int options)
53+
{
54+
if (!options)
55+
return -1;
56+
57+
if (level > LOG_DEBUG)
58+
return -1;
59+
60+
if (!ident)
61+
return -1;
62+
63+
__ident = ident;
64+
__options = options;
65+
66+
if (options & TO_SYSLOG) {
67+
openlog(__ident, options | LOG_NDELAY, LOG_USER);
68+
setlogmask(LOG_UPTO(level));
69+
}
70+
71+
return 0;
72+
}
73+
74+
void log_exit(void)
75+
{
76+
closelog();
77+
}

tools/thermal/lib/log.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* SPDX-License-Identifier: LGPL-2.1+ */
2+
/* Copyright (C) 2022, Linaro Ltd - Daniel Lezcano <daniel.lezcano@linaro.org> */
3+
#ifndef __THERMAL_TOOLS_LOG_H
4+
#define __THERMAL_TOOLS_LOG_H
5+
6+
#include <syslog.h>
7+
8+
#ifndef __maybe_unused
9+
#define __maybe_unused __attribute__((__unused__))
10+
#endif
11+
12+
#define TO_SYSLOG 0x1
13+
#define TO_STDOUT 0x2
14+
#define TO_STDERR 0x4
15+
16+
extern void logit(int level, const char *format, ...);
17+
18+
#define DEBUG(fmt, ...) logit(LOG_DEBUG, "%s:%d: " fmt, __func__, __LINE__, ##__VA_ARGS__)
19+
#define INFO(fmt, ...) logit(LOG_INFO, fmt, ##__VA_ARGS__)
20+
#define NOTICE(fmt, ...) logit(LOG_NOTICE, fmt, ##__VA_ARGS__)
21+
#define WARN(fmt, ...) logit(LOG_WARNING, fmt, ##__VA_ARGS__)
22+
#define ERROR(fmt, ...) logit(LOG_ERR, fmt, ##__VA_ARGS__)
23+
#define CRITICAL(fmt, ...) logit(LOG_CRIT, fmt, ##__VA_ARGS__)
24+
#define ALERT(fmt, ...) logit(LOG_ALERT, fmt, ##__VA_ARGS__)
25+
#define EMERG(fmt, ...) logit(LOG_EMERG, fmt, ##__VA_ARGS__)
26+
27+
int log_init(int level, const char *ident, int options);
28+
int log_str2level(const char *lvl);
29+
void log_exit(void);
30+
31+
#endif

0 commit comments

Comments
 (0)