]> git.proxmox.com Git - mirror_zfs.git/commitdiff
zed: don't malloc() global zed_conf instance, optimise zed_conf layout
authorнаб <nabijaczleweli@nabijaczleweli.xyz>
Wed, 7 Apr 2021 13:38:22 +0000 (15:38 +0200)
committerBrian Behlendorf <behlendorf1@llnl.gov>
Sun, 11 Apr 2021 22:27:30 +0000 (15:27 -0700)
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 <behlendorf1@llnl.gov>
Signed-off-by: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz>
Closes #11860

cmd/zed/zed.c
cmd/zed/zed_conf.c
cmd/zed/zed_conf.h

index be1848ef1cbece376c85715c926c29cc56ef9f5c..e56b45fa72a9bb9d5d8df610fc0a6d60ca53db6e 100644 (file)
@@ -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);
 }
index 5f895109d9ff8c08fad740e9d4d591e2672846ad..9e67363f7f459121dadf2510bc2af821fa16cb42 100644 (file)
 #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);
 }
 
 /*
index c0691ddbc5c989f44c7d486bdc1b7d05c012dba7..b2dc09c51a69bf20603ccc0cd2341ec264e15066 100644 (file)
 #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 */