Lines Matching full:daemon

84 struct daemon {  struct
98 static struct daemon __daemon = { argument
103 "perf daemon {start|signal|stop|ping} [<options>]",
104 "perf daemon [<options>]",
115 static struct daemon_session *daemon__add_session(struct daemon *config, char *name) in daemon__add_session()
133 static struct daemon_session *daemon__find_session(struct daemon *daemon, char *name) in daemon__find_session() argument
137 list_for_each_entry(session, &daemon->sessions, list) { in daemon__find_session()
156 static int session_config(struct daemon *daemon, const char *var, const char *value) in session_config() argument
170 session = daemon__find_session(daemon, name); in session_config()
174 session = daemon__add_session(daemon, name); in session_config()
217 struct daemon *daemon = cb; in server_config() local
220 return session_config(daemon, var, value); in server_config()
221 } else if (!strcmp(var, "daemon.base") && !daemon->base_user) { in server_config()
222 if (daemon->base && strcmp(daemon->base, value)) { in server_config()
226 daemon->base = strdup(value); in server_config()
227 if (!daemon->base) in server_config()
236 struct daemon *daemon = cb; in client_config() local
238 if (!strcmp(var, "daemon.base") && !daemon->base_user) { in client_config()
239 daemon->base = strdup(value); in client_config()
240 if (!daemon->base) in client_config()
247 static int check_base(struct daemon *daemon) in check_base() argument
251 if (!daemon->base) { in check_base()
256 if (stat(daemon->base, &st)) { in check_base()
260 daemon->base); in check_base()
264 daemon->base); in check_base()
268 daemon->base, strerror(errno)); in check_base()
275 daemon->base); in check_base()
282 static int setup_client_config(struct daemon *daemon) in setup_client_config() argument
284 struct perf_config_set *set = perf_config_set__load_file(daemon->config_real); in setup_client_config()
288 err = perf_config_set(set, client_config, daemon); in setup_client_config()
292 return err ?: check_base(daemon); in setup_client_config()
295 static int setup_server_config(struct daemon *daemon) in setup_server_config() argument
308 list_for_each_entry(session, &daemon->sessions, list) in setup_server_config()
311 set = perf_config_set__load_file(daemon->config_real); in setup_server_config()
313 err = perf_config_set(set, server_config, daemon); in setup_server_config()
317 return err ?: check_base(daemon); in setup_server_config()
321 struct daemon *daemon) in daemon_session__run() argument
328 daemon->base, session->name) < 0) { in daemon_session__run()
384 daemon->perf, SESSION_CONTROL, SESSION_ACK, session->run); in daemon_session__run()
390 exit(execve(daemon->perf, argv, NULL)); in daemon_session__run()
394 static pid_t handle_signalfd(struct daemon *daemon) in handle_signalfd() argument
408 err = read(daemon->signal_fd, &si, sizeof(struct signalfd_siginfo)); in handle_signalfd()
414 list_for_each_entry(session, &daemon->sessions, list) { in handle_signalfd()
443 static int daemon_session__wait(struct daemon_session *session, struct daemon *daemon, in daemon_session__wait() argument
447 .fd = daemon->signal_fd, in daemon_session__wait()
458 handle_signalfd(daemon); in daemon_session__wait()
471 static bool daemon__has_alive_session(struct daemon *daemon) in daemon__has_alive_session() argument
475 list_for_each_entry(session, &daemon->sessions, list) { in daemon__has_alive_session()
483 static int daemon__wait(struct daemon *daemon, int secs) in daemon__wait() argument
486 .fd = daemon->signal_fd, in daemon__wait()
497 handle_signalfd(daemon); in daemon__wait()
505 } while (daemon__has_alive_session(daemon)); in daemon__wait()
581 static int setup_server_socket(struct daemon *daemon) in setup_server_socket() argument
598 scnprintf(path, sizeof(path), "%s/control", daemon->base); in setup_server_socket()
672 static int cmd_session_list(struct daemon *daemon, union cmd *cmd, FILE *out) in cmd_session_list() argument
680 /* pid daemon */ in cmd_session_list()
681 getpid(), csv_sep, "daemon", in cmd_session_list()
683 csv_sep, daemon->base, in cmd_session_list()
685 csv_sep, daemon->base, SESSION_OUTPUT); in cmd_session_list()
689 csv_sep, daemon->base, "lock"); in cmd_session_list()
693 csv_sep, (curr - daemon->start) / 60); in cmd_session_list()
697 fprintf(out, "[%d:daemon] base: %s\n", getpid(), daemon->base); in cmd_session_list()
700 daemon->base, SESSION_OUTPUT); in cmd_session_list()
702 daemon->base); in cmd_session_list()
704 (curr - daemon->start) / 60); in cmd_session_list()
708 list_for_each_entry(session, &daemon->sessions, list) { in cmd_session_list()
763 static int cmd_session_kill(struct daemon *daemon, union cmd *cmd, FILE *out) in cmd_session_kill() argument
770 list_for_each_entry(session, &daemon->sessions, list) { in cmd_session_kill()
786 static int cmd_session_ping(struct daemon *daemon, union cmd *cmd, FILE *out) in cmd_session_ping() argument
793 list_for_each_entry(session, &daemon->sessions, list) { in cmd_session_ping()
809 static int handle_server_socket(struct daemon *daemon, int sock_fd) in handle_server_socket() argument
834 ret = cmd_session_list(daemon, &cmd, out); in handle_server_socket()
837 ret = cmd_session_kill(daemon, &cmd, out); in handle_server_socket()
842 pr_debug("perf daemon is exciting\n"); in handle_server_socket()
845 ret = cmd_session_ping(daemon, &cmd, out); in handle_server_socket()
859 static int setup_client_socket(struct daemon *daemon) in setup_client_socket() argument
870 scnprintf(path, sizeof(path), "%s/control", daemon->base); in setup_client_socket()
892 struct daemon *daemon) in daemon_session__kill() argument
914 } while (daemon_session__wait(session, daemon, 10)); in daemon_session__kill()
917 static void daemon__signal(struct daemon *daemon, int sig) in daemon__signal() argument
921 list_for_each_entry(session, &daemon->sessions, list) in daemon__signal()
939 static void daemon__stop(struct daemon *daemon) in daemon__stop() argument
943 list_for_each_entry(session, &daemon->sessions, list) in daemon__stop()
947 static void daemon__kill(struct daemon *daemon) in daemon__kill() argument
954 daemon__stop(daemon); in daemon__kill()
957 daemon__signal(daemon, SIGTERM); in daemon__kill()
960 daemon__signal(daemon, SIGKILL); in daemon__kill()
968 } while (daemon__wait(daemon, 10)); in daemon__kill()
971 static void daemon__exit(struct daemon *daemon) in daemon__exit() argument
975 list_for_each_entry_safe(session, h, &daemon->sessions, list) in daemon__exit()
978 zfree(&daemon->config_real); in daemon__exit()
979 zfree(&daemon->config_base); in daemon__exit()
980 zfree(&daemon->base); in daemon__exit()
983 static int daemon__reconfig(struct daemon *daemon) in daemon__reconfig() argument
987 list_for_each_entry_safe(session, n, &daemon->sessions, list) { in daemon__reconfig()
995 daemon_session__kill(session, daemon); in daemon__reconfig()
1004 daemon_session__kill(session, daemon); in daemon__reconfig()
1007 if (daemon_session__run(session, daemon)) in daemon__reconfig()
1016 static int setup_config_changes(struct daemon *daemon) in setup_config_changes() argument
1018 char *basen = strdup(daemon->config_real); in setup_config_changes()
1019 char *dirn = strdup(daemon->config_real); in setup_config_changes()
1038 daemon->config_base = strdup(base); in setup_config_changes()
1039 if (!daemon->config_base) { in setup_config_changes()
1053 static bool process_inotify_event(struct daemon *daemon, char *buf, ssize_t len) in process_inotify_event() argument
1066 if (!strcmp(event->name, daemon->config_base)) in process_inotify_event()
1074 static int handle_config_changes(struct daemon *daemon, int conf_fd, in handle_config_changes() argument
1089 *config_changed = process_inotify_event(daemon, buf, len); in handle_config_changes()
1094 static int setup_config(struct daemon *daemon) in setup_config() argument
1096 if (daemon->base_user) { in setup_config()
1097 daemon->base = strdup(daemon->base_user); in setup_config()
1098 if (!daemon->base) in setup_config()
1102 if (daemon->config) { in setup_config()
1103 char *real = realpath(daemon->config, NULL); in setup_config()
1109 daemon->config_real = real; in setup_config()
1114 daemon->config_real = strdup(perf_etc_perfconfig()); in setup_config()
1116 daemon->config_real = strdup(perf_home_perfconfig()); in setup_config()
1118 return daemon->config_real ? 0 : -1; in setup_config()
1134 * Each daemon tries to create and lock BASE/lock file,
1135 * if it's successful we are sure we're the only daemon
1138 * Once daemon is finished, file descriptor to lock file
1141 static int check_lock(struct daemon *daemon) in check_lock() argument
1148 scnprintf(path, sizeof(path), "%s/lock", daemon->base); in check_lock()
1156 fprintf(stderr, "failed: another perf daemon (pid %d) owns %s\n", in check_lock()
1157 pid, daemon->base); in check_lock()
1180 static int go_background(struct daemon *daemon) in go_background() argument
1194 if (check_lock(daemon)) in go_background()
1199 if (chdir(daemon->base)) { in go_background()
1221 daemon->out = fdopen(1, "w"); in go_background()
1222 if (!daemon->out) { in go_background()
1228 setbuf(daemon->out, NULL); in go_background()
1232 static int setup_signalfd(struct daemon *daemon) in setup_signalfd() argument
1242 daemon->signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC); in setup_signalfd()
1243 return daemon->signal_fd; in setup_signalfd()
1246 static int __cmd_start(struct daemon *daemon, struct option parent_options[], in __cmd_start() argument
1264 daemon->start = time(NULL); in __cmd_start()
1266 if (setup_config(daemon)) { in __cmd_start()
1271 if (setup_server_config(daemon)) in __cmd_start()
1274 if (foreground && check_lock(daemon)) in __cmd_start()
1278 err = go_background(daemon); in __cmd_start()
1283 daemon__exit(daemon); in __cmd_start()
1288 debug_set_file(daemon->out); in __cmd_start()
1291 pr_info("daemon started (pid %d)\n", getpid()); in __cmd_start()
1295 sock_fd = setup_server_socket(daemon); in __cmd_start()
1299 conf_fd = setup_config_changes(daemon); in __cmd_start()
1303 signal_fd = setup_signalfd(daemon); in __cmd_start()
1324 err = daemon__reconfig(daemon); in __cmd_start()
1330 err = handle_server_socket(daemon, sock_fd); in __cmd_start()
1332 err = handle_config_changes(daemon, conf_fd, &reconfig); in __cmd_start()
1334 err = handle_signalfd(daemon) < 0; in __cmd_start()
1337 err = setup_server_config(daemon); in __cmd_start()
1344 daemon__kill(daemon); in __cmd_start()
1345 daemon__exit(daemon); in __cmd_start()
1354 pr_info("daemon exited\n"); in __cmd_start()
1355 fclose(daemon->out); in __cmd_start()
1359 static int send_cmd(struct daemon *daemon, union cmd *cmd) in send_cmd() argument
1367 if (setup_client_config(daemon)) in send_cmd()
1370 fd = setup_client_socket(daemon); in send_cmd()
1402 static int send_cmd_list(struct daemon *daemon) in send_cmd_list() argument
1409 cmd.list.csv_sep = daemon->csv_sep ? *daemon->csv_sep : 0; in send_cmd_list()
1411 return send_cmd(daemon, &cmd); in send_cmd_list()
1414 static int __cmd_signal(struct daemon *daemon, struct option parent_options[], in __cmd_signal() argument
1430 if (setup_config(daemon)) { in __cmd_signal()
1440 return send_cmd(daemon, &cmd); in __cmd_signal()
1443 static int __cmd_stop(struct daemon *daemon, struct option parent_options[], in __cmd_stop() argument
1456 if (setup_config(daemon)) { in __cmd_stop()
1463 return send_cmd(daemon, &cmd); in __cmd_stop()
1466 static int __cmd_ping(struct daemon *daemon, struct option parent_options[], in __cmd_ping() argument
1482 if (setup_config(daemon)) { in __cmd_ping()
1490 return send_cmd(daemon, &cmd); in __cmd_ping()