From 32cc3f0837f9ea959fb06bfcb8bdb658a585d2bb Mon Sep 17 00:00:00 2001 From: наб Date: Wed, 7 Apr 2021 15:38:22 +0200 Subject: zed: don't malloc() global zed_conf instance, optimise zed_conf layout MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It's all of 40 bytes with 4-byte pointers and 64 with 8-byte ones (previously 44 and 88, respectively) ‒ there's no reason it can't live on the stack Reviewed-by: Brian Behlendorf Signed-off-by: Ahelenia Ziemiańska Closes #11860 --- cmd/zed/zed.c | 38 +++++++++++++++++++------------------- cmd/zed/zed_conf.c | 44 ++++++++++++++------------------------------ cmd/zed/zed_conf.h | 29 +++++++++++++++-------------- 3 files changed, 48 insertions(+), 63 deletions(-) diff --git a/cmd/zed/zed.c b/cmd/zed/zed.c index be1848ef1..e56b45fa7 100644 --- a/cmd/zed/zed.c +++ b/cmd/zed/zed.c @@ -216,15 +216,15 @@ _finish_daemonize(void) int main(int argc, char *argv[]) { - struct zed_conf *zcp; + struct zed_conf zcp; uint64_t saved_eid; int64_t saved_etime[2]; zed_log_init(argv[0]); zed_log_stderr_open(LOG_NOTICE); - zcp = zed_conf_create(); - zed_conf_parse_opts(zcp, argc, argv); - if (zcp->do_verbose) + zed_conf_init(&zcp); + zed_conf_parse_opts(&zcp, argc, argv); + if (zcp.do_verbose) zed_log_stderr_open(LOG_INFO); if (geteuid() != 0) @@ -237,32 +237,32 @@ main(int argc, char *argv[]) if (chdir("/") < 0) zed_log_die("Failed to change to root directory"); - if (zed_conf_scan_dir(zcp) < 0) + if (zed_conf_scan_dir(&zcp) < 0) exit(EXIT_FAILURE); - if (!zcp->do_foreground) { + if (!zcp.do_foreground) { _start_daemonize(); zed_log_syslog_open(LOG_DAEMON); } _setup_sig_handlers(); - if (zcp->do_memlock) + if (zcp.do_memlock) _lock_memory(); - if ((zed_conf_write_pid(zcp) < 0) && (!zcp->do_force)) + if ((zed_conf_write_pid(&zcp) < 0) && (!zcp.do_force)) exit(EXIT_FAILURE); - if (!zcp->do_foreground) + if (!zcp.do_foreground) _finish_daemonize(); zed_log_msg(LOG_NOTICE, "ZFS Event Daemon %s-%s (PID %d)", ZFS_META_VERSION, ZFS_META_RELEASE, (int)getpid()); - if (zed_conf_open_state(zcp) < 0) + if (zed_conf_open_state(&zcp) < 0) exit(EXIT_FAILURE); - if (zed_conf_read_state(zcp, &saved_eid, saved_etime) < 0) + if (zed_conf_read_state(&zcp, &saved_eid, saved_etime) < 0) exit(EXIT_FAILURE); idle: @@ -271,24 +271,24 @@ idle: * successful. */ do { - if (!zed_event_init(zcp)) + if (!zed_event_init(&zcp)) break; /* Wait for some time and try again. tunable? */ sleep(30); - } while (!_got_exit && zcp->do_idle); + } while (!_got_exit && zcp.do_idle); if (_got_exit) goto out; - zed_event_seek(zcp, saved_eid, saved_etime); + zed_event_seek(&zcp, saved_eid, saved_etime); while (!_got_exit) { int rv; if (_got_hup) { _got_hup = 0; - (void) zed_conf_scan_dir(zcp); + (void) zed_conf_scan_dir(&zcp); } - rv = zed_event_service(zcp); + rv = zed_event_service(&zcp); /* ENODEV: When kernel module is unloaded (osx) */ if (rv == ENODEV) @@ -296,13 +296,13 @@ idle: } zed_log_msg(LOG_NOTICE, "Exiting"); - zed_event_fini(zcp); + zed_event_fini(&zcp); - if (zcp->do_idle && !_got_exit) + if (zcp.do_idle && !_got_exit) goto idle; out: - zed_conf_destroy(zcp); + zed_conf_destroy(&zcp); zed_log_fini(); exit(EXIT_SUCCESS); } diff --git a/cmd/zed/zed_conf.c b/cmd/zed/zed_conf.c index 5f895109d..9e67363f7 100644 --- a/cmd/zed/zed_conf.c +++ b/cmd/zed/zed_conf.c @@ -32,38 +32,26 @@ #include "zed_strings.h" /* - * Return a new configuration with default values. + * Initialise the configuration with default values. */ -struct zed_conf * -zed_conf_create(void) +void +zed_conf_init(struct zed_conf *zcp) { - struct zed_conf *zcp; - - zcp = calloc(1, sizeof (*zcp)); - if (!zcp) - goto nomem; - - zcp->pid_fd = -1; - zcp->zedlets = NULL; /* created via zed_conf_scan_dir() */ - zcp->state_fd = -1; /* opened via zed_conf_open_state() */ - zcp->zfs_hdl = NULL; /* opened via zed_event_init() */ - zcp->zevent_fd = -1; /* opened via zed_event_init() */ - zcp->max_jobs = 16; + memset(zcp, 0, sizeof (*zcp)); - if (!(zcp->pid_file = strdup(ZED_PID_FILE))) - goto nomem; + /* zcp->zfs_hdl opened in zed_event_init() */ + /* zcp->zedlets created in zed_conf_scan_dir() */ - if (!(zcp->zedlet_dir = strdup(ZED_ZEDLET_DIR))) - goto nomem; + zcp->pid_fd = -1; /* opened in zed_conf_write_pid() */ + zcp->state_fd = -1; /* opened in zed_conf_open_state() */ + zcp->zevent_fd = -1; /* opened in zed_event_init() */ - if (!(zcp->state_file = strdup(ZED_STATE_FILE))) - goto nomem; - - return (zcp); + zcp->max_jobs = 16; -nomem: - zed_log_die("Failed to create conf: %s", strerror(errno)); - return (NULL); + if (!(zcp->pid_file = strdup(ZED_PID_FILE)) || + !(zcp->zedlet_dir = strdup(ZED_ZEDLET_DIR)) || + !(zcp->state_file = strdup(ZED_STATE_FILE))) + zed_log_die("Failed to create conf: %s", strerror(errno)); } /* @@ -74,9 +62,6 @@ nomem: void zed_conf_destroy(struct zed_conf *zcp) { - if (!zcp) - return; - if (zcp->state_fd >= 0) { if (close(zcp->state_fd) < 0) zed_log_msg(LOG_WARNING, @@ -113,7 +98,6 @@ zed_conf_destroy(struct zed_conf *zcp) zed_strings_destroy(zcp->zedlets); zcp->zedlets = NULL; } - free(zcp); } /* diff --git a/cmd/zed/zed_conf.h b/cmd/zed/zed_conf.h index c0691ddbc..b2dc09c51 100644 --- a/cmd/zed/zed_conf.h +++ b/cmd/zed/zed_conf.h @@ -20,26 +20,29 @@ #include "zed_strings.h" struct zed_conf { - unsigned do_force:1; /* true if force enabled */ - unsigned do_foreground:1; /* true if run in foreground */ - unsigned do_memlock:1; /* true if locking memory */ - unsigned do_verbose:1; /* true if verbosity enabled */ - unsigned do_zero:1; /* true if zeroing state */ - unsigned do_idle:1; /* true if idle enabled */ char *pid_file; /* abs path to pid file */ - int pid_fd; /* fd to pid file for lock */ char *zedlet_dir; /* abs path to zedlet dir */ - zed_strings_t *zedlets; /* names of enabled zedlets */ char *state_file; /* abs path to state file */ - int state_fd; /* fd to state file */ + libzfs_handle_t *zfs_hdl; /* handle to libzfs */ - int zevent_fd; /* fd for access to zevents */ + zed_strings_t *zedlets; /* names of enabled zedlets */ char *path; /* custom $PATH for zedlets to use */ + + int pid_fd; /* fd to pid file for lock */ + int state_fd; /* fd to state file */ + int zevent_fd; /* fd for access to zevents */ + int16_t max_jobs; /* max zedlets to run at one time */ -}; -struct zed_conf *zed_conf_create(void); + boolean_t do_force:1; /* true if force enabled */ + boolean_t do_foreground:1; /* true if run in foreground */ + boolean_t do_memlock:1; /* true if locking memory */ + boolean_t do_verbose:1; /* true if verbosity enabled */ + boolean_t do_zero:1; /* true if zeroing state */ + boolean_t do_idle:1; /* true if idle enabled */ +}; +void zed_conf_init(struct zed_conf *zcp); void zed_conf_destroy(struct zed_conf *zcp); void zed_conf_parse_opts(struct zed_conf *zcp, int argc, char **argv); @@ -49,9 +52,7 @@ int zed_conf_scan_dir(struct zed_conf *zcp); int zed_conf_write_pid(struct zed_conf *zcp); int zed_conf_open_state(struct zed_conf *zcp); - int zed_conf_read_state(struct zed_conf *zcp, uint64_t *eidp, int64_t etime[]); - int zed_conf_write_state(struct zed_conf *zcp, uint64_t eid, int64_t etime[]); #endif /* !ZED_CONF_H */ -- cgit v1.2.3