mirror of
https://github.com/pikvm/ustreamer.git
synced 2026-10-16 18:54:13 +00:00
governor scaling options
This commit is contained in:
@@ -156,6 +156,9 @@ enum _US_OPT_VALUES {
|
|||||||
# endif
|
# endif
|
||||||
_O_NOTIFY_PARENT,
|
_O_NOTIFY_PARENT,
|
||||||
|
|
||||||
|
_O_CPU_SCALING_GOVERNOR_IDLE,
|
||||||
|
_O_CPU_SCALING_GOVERNOR_ACTIVE,
|
||||||
|
|
||||||
_O_LOG_LEVEL,
|
_O_LOG_LEVEL,
|
||||||
_O_PERF,
|
_O_PERF,
|
||||||
_O_VERBOSE,
|
_O_VERBOSE,
|
||||||
@@ -270,6 +273,9 @@ static const struct option _LONG_OPTS[] = {
|
|||||||
# endif
|
# endif
|
||||||
{"notify-parent", no_argument, NULL, _O_NOTIFY_PARENT},
|
{"notify-parent", no_argument, NULL, _O_NOTIFY_PARENT},
|
||||||
|
|
||||||
|
{"cpu-scaling-governor-idle", required_argument, NULL, _O_CPU_SCALING_GOVERNOR_IDLE},
|
||||||
|
{"cpu-scaling-governor-active", required_argument, NULL, _O_CPU_SCALING_GOVERNOR_ACTIVE},
|
||||||
|
|
||||||
{"log-level", required_argument, NULL, _O_LOG_LEVEL},
|
{"log-level", required_argument, NULL, _O_LOG_LEVEL},
|
||||||
{"perf", no_argument, NULL, _O_PERF},
|
{"perf", no_argument, NULL, _O_PERF},
|
||||||
{"verbose", no_argument, NULL, _O_VERBOSE},
|
{"verbose", no_argument, NULL, _O_VERBOSE},
|
||||||
@@ -550,6 +556,9 @@ int us_options_parse(
|
|||||||
# endif
|
# endif
|
||||||
case _O_NOTIFY_PARENT: OPT_SET(stream->notify_parent, true);
|
case _O_NOTIFY_PARENT: OPT_SET(stream->notify_parent, true);
|
||||||
|
|
||||||
|
case _O_CPU_SCALING_GOVERNOR_IDLE: OPT_SET(stream->governor_idle, optarg);
|
||||||
|
case _O_CPU_SCALING_GOVERNOR_ACTIVE: OPT_SET(stream->governor_active, optarg);
|
||||||
|
|
||||||
case _O_LOG_LEVEL: OPT_NUMBER("--log-level", us_g_log_level, US_LOG_LEVEL_INFO, US_LOG_LEVEL_DEBUG, 0);
|
case _O_LOG_LEVEL: OPT_NUMBER("--log-level", us_g_log_level, US_LOG_LEVEL_INFO, US_LOG_LEVEL_DEBUG, 0);
|
||||||
case _O_PERF: OPT_SET(us_g_log_level, US_LOG_LEVEL_PERF);
|
case _O_PERF: OPT_SET(us_g_log_level, US_LOG_LEVEL_PERF);
|
||||||
case _O_VERBOSE: OPT_SET(us_g_log_level, US_LOG_LEVEL_VERBOSE);
|
case _O_VERBOSE: OPT_SET(us_g_log_level, US_LOG_LEVEL_VERBOSE);
|
||||||
|
|||||||
@@ -94,7 +94,8 @@ static void _stream_drm_ensure_no_signal(us_stream_s *stream);
|
|||||||
static void _stream_expose_jpeg(us_stream_s *stream, const us_frame_s *frame);
|
static void _stream_expose_jpeg(us_stream_s *stream, const us_frame_s *frame);
|
||||||
static void _stream_expose_raw(us_stream_s *stream, const us_frame_s *frame);
|
static void _stream_expose_raw(us_stream_s *stream, const us_frame_s *frame);
|
||||||
static void _stream_encode_expose_h264(us_stream_s *stream, const us_frame_s *frame, bool force_key);
|
static void _stream_encode_expose_h264(us_stream_s *stream, const us_frame_s *frame, bool force_key);
|
||||||
static void _stream_check_suicide(us_stream_s *stream);
|
static void _stream_check_suicide(us_stream_s *stream, bool has_any_clients);
|
||||||
|
static void _stream_set_governor(us_stream_s *stream, bool active);
|
||||||
|
|
||||||
|
|
||||||
us_stream_s *us_stream_init(us_capture_s *cap, us_encoder_s *enc) {
|
us_stream_s *us_stream_init(us_capture_s *cap, us_encoder_s *enc) {
|
||||||
@@ -204,7 +205,7 @@ void us_stream_loop(us_stream_s *stream) {
|
|||||||
|
|
||||||
US_LOG_INFO("Capturing ...");
|
US_LOG_INFO("Capturing ...");
|
||||||
|
|
||||||
uint slowdown_count = 0;
|
int has_any_clients_prev = -1;
|
||||||
while (!atomic_load(&run->stop) && !atomic_load(&threads_stop)) {
|
while (!atomic_load(&run->stop) && !atomic_load(&threads_stop)) {
|
||||||
us_capture_hwbuf_s *hw;
|
us_capture_hwbuf_s *hw;
|
||||||
switch (us_capture_hwbuf_grab(cap, &hw)) {
|
switch (us_capture_hwbuf_grab(cap, &hw)) {
|
||||||
@@ -233,17 +234,23 @@ void us_stream_loop(us_stream_s *stream) {
|
|||||||
us_queue_put(releasers[hw->buf.index].q, hw, 0); // Plan to release
|
us_queue_put(releasers[hw->buf.index].q, hw, 0); // Plan to release
|
||||||
|
|
||||||
// Мы не обновляем здесь состояние синков, потому что это происходит внутри обслуживающих их потоков
|
// Мы не обновляем здесь состояние синков, потому что это происходит внутри обслуживающих их потоков
|
||||||
_stream_check_suicide(stream);
|
const bool has_any_clients = _stream_has_any_clients_cached(stream);
|
||||||
if (stream->slowdown && !_stream_has_any_clients_cached(stream)) {
|
|
||||||
usleep(100 * 1000);
|
if (has_any_clients_prev != has_any_clients) {
|
||||||
slowdown_count = (slowdown_count + 1) % 10;
|
_stream_set_governor(stream, has_any_clients);
|
||||||
if (slowdown_count > 0) {
|
has_any_clients_prev = has_any_clients;
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_stream_check_suicide(stream, has_any_clients);
|
||||||
|
|
||||||
|
if (stream->slowdown && !has_any_clients) {
|
||||||
|
usleep(100 * 1000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
close:
|
close:
|
||||||
|
_stream_set_governor(stream, false);
|
||||||
|
|
||||||
atomic_store(&threads_stop, true);
|
atomic_store(&threads_stop, true);
|
||||||
|
|
||||||
# define DELETE_WORKER(x_ctx) if (x_ctx != NULL) { \
|
# define DELETE_WORKER(x_ctx) if (x_ctx != NULL) { \
|
||||||
@@ -571,7 +578,7 @@ static int _stream_init_loop(us_stream_s *stream) {
|
|||||||
UPDATE_SINK(stream->h264_sink);
|
UPDATE_SINK(stream->h264_sink);
|
||||||
# undef UPDATE_SINK
|
# undef UPDATE_SINK
|
||||||
|
|
||||||
_stream_check_suicide(stream);
|
_stream_check_suicide(stream, _stream_has_any_clients_cached(stream));
|
||||||
|
|
||||||
stream->cap->dma_export = (
|
stream->cap->dma_export = (
|
||||||
stream->enc->type == US_ENCODER_TYPE_M2M_VIDEO
|
stream->enc->type == US_ENCODER_TYPE_M2M_VIDEO
|
||||||
@@ -767,7 +774,7 @@ done:
|
|||||||
us_fpsi_update(run->http->h264_fpsi, meta.online, &meta);
|
us_fpsi_update(run->http->h264_fpsi, meta.online, &meta);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void _stream_check_suicide(us_stream_s *stream) {
|
static void _stream_check_suicide(us_stream_s *stream, bool has_any_clients) {
|
||||||
if (stream->exit_on_no_clients == 0) {
|
if (stream->exit_on_no_clients == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -775,7 +782,7 @@ static void _stream_check_suicide(us_stream_s *stream) {
|
|||||||
atomic_ullong *last_req_ts = &stream->run->http->last_req_ts;
|
atomic_ullong *last_req_ts = &stream->run->http->last_req_ts;
|
||||||
const ldf now_ts = us_get_now_monotonic();
|
const ldf now_ts = us_get_now_monotonic();
|
||||||
|
|
||||||
if (_stream_has_any_clients_cached(stream)) {
|
if (has_any_clients) {
|
||||||
atomic_store(last_req_ts, now_ts);
|
atomic_store(last_req_ts, now_ts);
|
||||||
} else if (atomic_load(last_req_ts) + stream->exit_on_no_clients < now_ts) {
|
} else if (atomic_load(last_req_ts) + stream->exit_on_no_clients < now_ts) {
|
||||||
US_LOG_INFO("No requests or HTTP/sink clients found in last %u seconds, exiting ...",
|
US_LOG_INFO("No requests or HTTP/sink clients found in last %u seconds, exiting ...",
|
||||||
@@ -784,3 +791,26 @@ static void _stream_check_suicide(us_stream_s *stream) {
|
|||||||
us_process_suicide();
|
us_process_suicide();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void _stream_set_governor(us_stream_s *stream, bool active) {
|
||||||
|
if (!us_str_is_ok(stream->governor_idle) || !us_str_is_ok(stream->governor_active)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *const path = "/sys/devices/system/cpu/cpufreq/policy0/scaling_governor";
|
||||||
|
const int fd = open(path, O_RDWR);
|
||||||
|
if (fd < 0) {
|
||||||
|
US_LOG_PERROR("Can't open CPU governor file");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *const gov = (active ? stream->governor_active : stream->governor_idle);
|
||||||
|
const sz len = strlen(gov);
|
||||||
|
if (write(fd, gov, len) != len) {
|
||||||
|
US_LOG_PERROR("Can't set CPU governor to %s", gov);
|
||||||
|
} else {
|
||||||
|
US_LOG_INFO("CPU governor changed to %s", gov);
|
||||||
|
}
|
||||||
|
|
||||||
|
close(fd);
|
||||||
|
}
|
||||||
|
|||||||
@@ -87,6 +87,9 @@ typedef struct {
|
|||||||
bool exit_on_device_error;
|
bool exit_on_device_error;
|
||||||
uint exit_on_no_clients;
|
uint exit_on_no_clients;
|
||||||
|
|
||||||
|
char *governor_idle;
|
||||||
|
char *governor_active;
|
||||||
|
|
||||||
us_memsink_s *jpeg_sink;
|
us_memsink_s *jpeg_sink;
|
||||||
us_memsink_s *raw_sink;
|
us_memsink_s *raw_sink;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user