[Openais] [PATCH corosync/trunk] add a log_printf function to coroipcs so we can pass log level
Steven Dake
sdake at redhat.com
Thu Oct 15 21:28:24 PDT 2009
On Fri, 2009-10-16 at 14:25 +1300, angus salkeld wrote:
> On Thu, 2009-10-15 at 15:32 -0700, Steven Dake wrote:
> > Angus,
> >
> > What I would like to see are two apis:
> >
> > one takes coroipcs_init_state. The other API takes a
> > coroipcs_init_state_v2. All apis in state are also members of state_v2.
> >
> > Inside coroipcs, then the user would call things like
> >
> > api->malloc
> > api->log_printf
> > api->stats_increment_value
> >
> > the internal api would be loaded based upon whatever init function was
> > called.
> >
> > Regards
> > -steve
> Hopefully this will make everyone happy.
>
> In main.c you can load the version 1 api by defining the following:
> /* #define USE_IPC_API_V1 1 */
>
nuke this part about the define and the patch is good to go.
Also tabs are not aligned.
Regards
-steve
> You can confirm that you get a log message like this:
> Oct 16 14:12:08 corosync [IPC ] coroipcs.c:937 you are using ipc api v2
> or (with it undefined)
> Oct 16 14:10:37 corosync [IPC ] main.c:889 you are using ipc api v1
> (note the file:linenumber)
>
> and version 1 you don't get stats.
>
> Once you are happy with things I can delete the version 1 stuff from main.c
>
> Regards
> Angus
>
>
> Index: include/corosync/coroipcs.h
> ===================================================================
> --- include/corosync/coroipcs.h (revision 2529)
> +++ include/corosync/coroipcs.h (working copy)
> @@ -75,20 +75,53 @@
> coroipcs_handler_fn_lvalue (*handler_fn_get)(unsigned int service, unsigned int id);
> };
>
> -struct coroipcs_init_stats_state {
> +struct coroipcs_init_state_v2 {
> + const char *socket_name;
> + int sched_policy;
> + const struct sched_param *sched_param;
> + void *(*malloc) (size_t size);
> + void (*free) (void *ptr);
> + void (*old_log_printf) (
> + const char *format,
> + ...) __attribute__((format(printf, 1, 2)));
> + int (*service_available)(unsigned int service);
> + int (*private_data_size_get)(unsigned int service);
> + int (*security_valid)(int uid, int gid);
> + void (*serialize_lock)(void);
> + void (*serialize_unlock)(void);
> + int (*sending_allowed)(unsigned int service, unsigned int id,
> + const void *msg, void *sending_allowed_private_data);
> + void (*sending_allowed_release)(void *sending_allowed_private_data);
> + void (*poll_accept_add)(int fd);
> + void (*poll_dispatch_add)(int fd, void *context);
> + void (*poll_dispatch_modify)(int fd, int events);
> + void (*poll_dispatch_destroy)(int fd, void *context);
> + void (*fatal_error)(const char *error_msg);
> + coroipcs_init_fn_lvalue (*init_fn_get)(unsigned int service);
> + coroipcs_exit_fn_lvalue (*exit_fn_get)(unsigned int service);
> + coroipcs_handler_fn_lvalue (*handler_fn_get)(unsigned int service, unsigned int id);
> + /* v2 functions */
> hdb_handle_t (*stats_create_connection) (const char* name,
> pid_t pid, int fd);
> void (*stats_destroy_connection) (hdb_handle_t handle);
> void (*stats_update_value) (hdb_handle_t handle,
> const char *name, const void *value, size_t value_len);
> void (*stats_increment_value) (hdb_handle_t handle, const char* name);
> + void (*log_printf) (
> + unsigned int rec_ident,
> + const char *function_name,
> + const char *file_name,
> + int file_line,
> + const char *format,
> + ...) __attribute__((format(printf, 5, 6)));
> + int log_subsys_id;
> };
>
> extern void coroipcs_ipc_init (
> struct coroipcs_init_state *init_state);
>
> -extern void coroipcs_ipc_stats_init (
> - struct coroipcs_init_stats_state *init_stats_state);
> +extern void coroipcs_ipc_init_v2 (
> + struct coroipcs_init_state_v2 *init_state_v2);
>
> extern void *coroipcs_private_data_get (void *conn);
>
> Index: exec/coroipcs.c
> ===================================================================
> --- exec/coroipcs.c (revision 2529)
> +++ exec/coroipcs.c (working copy)
> @@ -76,6 +76,9 @@
> #include <corosync/coroipcs.h>
> #include <corosync/coroipc_ipc.h>
>
> +#define LOGSYS_UTILS_ONLY 1
> +#include <corosync/engine/logsys.h>
> +
> #if _POSIX_THREAD_PROCESS_SHARED > 0
> #include <semaphore.h>
> #else
> @@ -91,8 +94,7 @@
> #define MSG_SEND_LOCKED 0
> #define MSG_SEND_UNLOCKED 1
>
> -static struct coroipcs_init_state *api;
> -static struct coroipcs_init_stats_state *stats_api;
> +static struct coroipcs_init_state_v2 *api = NULL;
>
> DECLARE_LIST_INIT (conn_info_list_head);
>
> @@ -173,6 +175,21 @@
> static void msg_send (void *conn, const struct iovec *iov, unsigned int iov_len,
> int locked);
>
> +static void _corosync_ipc_init(void);
> +
> +#define log_printf(level, format, args...) \
> +do { \
> + if (api->log_printf) \
> + api->log_printf ( \
> + LOGSYS_ENCODE_RECID(level, \
> + api->log_subsys_id, \
> + LOGSYS_RECID_LOG), \
> + __FUNCTION__, __FILE__, __LINE__, \
> + (const char *)format, ##args); \
> + else \
> + api->old_log_printf ((const char *)format, ##args); \
> +} while (0)
> +
> static hdb_handle_t dummy_stats_create_connection (
> const char *name,
> pid_t pid,
> @@ -200,13 +217,6 @@
> {
> }
>
> -static struct coroipcs_init_stats_state dummy_init_stats_state = {
> - .stats_create_connection = dummy_stats_create_connection,
> - .stats_destroy_connection = dummy_stats_destroy_connection,
> - .stats_update_value = dummy_stats_update_value,
> - .stats_increment_value = dummy_stats_increment_value
> -};
> -
> static void sem_post_exit_thread (struct conn_info *conn_info)
> {
> #if _POSIX_THREAD_PROCESS_SHARED < 1
> @@ -218,7 +228,7 @@
> retry_semop:
> res = sem_post (&conn_info->control_buffer->sem0);
> if (res == -1 && errno == EINTR) {
> - stats_api->stats_increment_value (conn_info->stats_handle, "sem_retry_count");
> + api->stats_increment_value (conn_info->stats_handle, "sem_retry_count");
> goto retry_semop;
> }
> #else
> @@ -229,7 +239,7 @@
> retry_semop:
> res = semop (conn_info->semid, &sop, 1);
> if ((res == -1) && (errno == EINTR || errno == EAGAIN)) {
> - stats_api->stats_increment_value (conn_info->stats_handle, "sem_retry_count");
> + api->stats_increment_value (conn_info->stats_handle, "sem_retry_count");
> goto retry_semop;
> }
> #endif
> @@ -477,7 +487,7 @@
> * Retry library exit function if busy
> */
> if (conn_info->state == CONN_STATE_THREAD_DESTROYED) {
> - stats_api->stats_destroy_connection (conn_info->stats_handle);
> + api->stats_destroy_connection (conn_info->stats_handle);
> res = api->exit_fn_get (conn_info->service) (conn_info);
> if (res == -1) {
> api->serialize_unlock ();
> @@ -629,7 +639,7 @@
> pthread_exit (0);
> }
> if ((res == -1) && (errno == EINTR)) {
> - stats_api->stats_increment_value (conn_info->stats_handle, "sem_retry_count");
> + api->stats_increment_value (conn_info->stats_handle, "sem_retry_count");
> goto retry_semwait;
> }
> #else
> @@ -644,7 +654,7 @@
> pthread_exit (0);
> }
> if ((res == -1) && (errno == EINTR || errno == EAGAIN)) {
> - stats_api->stats_increment_value (conn_info->stats_handle, "sem_retry_count");
> + api->stats_increment_value (conn_info->stats_handle, "sem_retry_count");
> goto retry_semop;
> } else
> if ((res == -1) && (errno == EINVAL || errno == EIDRM)) {
> @@ -682,14 +692,14 @@
> } else
> if (send_ok) {
> api->serialize_lock();
> - stats_api->stats_increment_value (conn_info->stats_handle, "requests");
> + api->stats_increment_value (conn_info->stats_handle, "requests");
> api->handler_fn_get (conn_info->service, header->id) (conn_info, header);
> api->serialize_unlock();
> } else {
> /*
> * Overload, tell library to retry
> */
> - stats_api->stats_increment_value (conn_info->stats_handle, "sem_retry_count");
> + api->stats_increment_value (conn_info->stats_handle, "sem_retry_count");
> coroipc_response_header.size = sizeof (coroipc_response_header_t);
> coroipc_response_header.id = 0;
> coroipc_response_header.error = CS_ERR_TRY_AGAIN;
> @@ -717,11 +727,11 @@
> retry_send:
> res = send (conn_info->fd, &res_setup, sizeof (mar_res_setup_t), MSG_WAITALL);
> if (res == -1 && errno == EINTR) {
> - stats_api->stats_increment_value (conn_info->stats_handle, "send_retry_count");
> + api->stats_increment_value (conn_info->stats_handle, "send_retry_count");
> goto retry_send;
> } else
> if (res == -1 && errno == EAGAIN) {
> - stats_api->stats_increment_value (conn_info->stats_handle, "send_retry_count");
> + api->stats_increment_value (conn_info->stats_handle, "send_retry_count");
> goto retry_send;
> }
> return (0);
> @@ -766,7 +776,7 @@
> retry_recv:
> res = recvmsg (conn_info->fd, &msg_recv, MSG_NOSIGNAL);
> if (res == -1 && errno == EINTR) {
> - stats_api->stats_increment_value (conn_info->stats_handle, "recv_retry_count");
> + api->stats_increment_value (conn_info->stats_handle, "recv_retry_count");
> goto retry_recv;
> } else
> if (res == -1 && errno != EAGAIN) {
> @@ -846,11 +856,11 @@
>
> #else /* no credentials */
> authenticated = 1;
> - api->log_printf ("Platform does not support IPC authentication. Using no authentication\n");
> + log_printf (LOGSYS_LEVEL_ERROR, "Platform does not support IPC authentication. Using no authentication\n");
> #endif /* no credentials */
>
> if (authenticated == 0) {
> - api->log_printf ("Invalid IPC credentials.\n");
> + log_printf (LOGSYS_LEVEL_ERROR, "Invalid IPC credentials.\n");
> ipc_disconnect (conn_info);
> return (-1);
> }
> @@ -917,17 +927,59 @@
> /*
> * Exported functions
> */
> +extern void coroipcs_ipc_init_v2 (
> + struct coroipcs_init_state_v2 *init_state_v2)
> +{
> + api = init_state_v2;
> + api->old_log_printf = NULL;
> +
> + log_printf (LOGSYS_LEVEL_DEBUG, "you are using ipc api v2\n");
> + _corosync_ipc_init ();
> +}
> +
> extern void coroipcs_ipc_init (
> struct coroipcs_init_state *init_state)
> {
> + api = calloc (sizeof(struct coroipcs_init_state_v2), 1);
> + /* v2 api */
> + api->stats_create_connection = dummy_stats_create_connection;
> + api->stats_destroy_connection = dummy_stats_destroy_connection;
> + api->stats_update_value = dummy_stats_update_value;
> + api->stats_increment_value = dummy_stats_increment_value;
> + api->log_printf = NULL;
> +
> + /* v1 api */
> + api->socket_name = init_state->socket_name;
> + api->sched_policy = init_state->sched_policy;
> + api->sched_param = init_state->sched_param;
> + api->malloc = init_state->malloc;
> + api->free = init_state->free;
> + api->old_log_printf = init_state->log_printf;
> + api->fatal_error = init_state->fatal_error;
> + api->security_valid = init_state->security_valid;
> + api->service_available = init_state->service_available;
> + api->private_data_size_get = init_state->private_data_size_get;
> + api->serialize_lock = init_state->serialize_lock;
> + api->serialize_unlock = init_state->serialize_unlock;
> + api->sending_allowed = init_state->sending_allowed;
> + api->sending_allowed_release = init_state->sending_allowed_release;
> + api->poll_accept_add = init_state->poll_accept_add;
> + api->poll_dispatch_add = init_state->poll_dispatch_add;
> + api->poll_dispatch_modify = init_state->poll_dispatch_modify;
> + api->init_fn_get = init_state->init_fn_get;
> + api->exit_fn_get = init_state->exit_fn_get;
> + api->handler_fn_get = init_state->handler_fn_get;
> +
> + log_printf (LOGSYS_LEVEL_DEBUG, "you are using ipc api v1\n");
> +
> + _corosync_ipc_init ();
> +}
> +
> +static void _corosync_ipc_init(void)
> +{
> int server_fd;
> struct sockaddr_un un_addr;
> int res;
> -
> - api = init_state;
> -
> - stats_api = &dummy_init_stats_state;
> -
> /*
> * Create socket for IPC clients, name socket, listen for connections
> */
> @@ -937,13 +989,13 @@
> server_fd = socket (PF_LOCAL, SOCK_STREAM, 0);
> #endif
> if (server_fd == -1) {
> - api->log_printf ("Cannot create client connections socket.\n");
> + log_printf (LOGSYS_LEVEL_CRIT, "Cannot create client connections socket.\n");
> api->fatal_error ("Can't create library listen socket");
> };
>
> res = fcntl (server_fd, F_SETFL, O_NONBLOCK);
> if (res == -1) {
> - api->log_printf ("Could not set non-blocking operation on server socket: %s\n", strerror (errno));
> + log_printf (LOGSYS_LEVEL_CRIT, "Could not set non-blocking operation on server socket: %s\n", strerror (errno));
> api->fatal_error ("Could not set non-blocking operation on server socket");
> }
>
> @@ -960,7 +1012,7 @@
> struct stat stat_out;
> res = stat (SOCKETDIR, &stat_out);
> if (res == -1 || (res == 0 && !S_ISDIR(stat_out.st_mode))) {
> - api->log_printf ("Required directory not present %s\n", SOCKETDIR);
> + log_printf (LOGSYS_LEVEL_CRIT, "Required directory not present %s\n", SOCKETDIR);
> api->fatal_error ("Please create required directory.");
> }
> sprintf (un_addr.sun_path, "%s/%s", SOCKETDIR, api->socket_name);
> @@ -970,7 +1022,7 @@
>
> res = bind (server_fd, (struct sockaddr *)&un_addr, COROSYNC_SUN_LEN(&un_addr));
> if (res) {
> - api->log_printf ("Could not bind AF_UNIX (%s): %s.\n", un_addr.sun_path, strerror (errno));
> + log_printf (LOGSYS_LEVEL_CRIT, "Could not bind AF_UNIX (%s): %s.\n", un_addr.sun_path, strerror (errno));
> api->fatal_error ("Could not bind to AF_UNIX socket\n");
> }
>
> @@ -989,12 +1041,6 @@
> api->poll_accept_add (server_fd);
> }
>
> -extern void coroipcs_ipc_stats_init (
> - struct coroipcs_init_stats_state *init_stats_state)
> -{
> - stats_api = init_stats_state;
> -}
> -
> void coroipcs_ipc_exit (void)
> {
> struct list_head *list;
> @@ -1063,14 +1109,14 @@
> retry_semop:
> res = semop (conn_info->semid, &sop, 1);
> if ((res == -1) && (errno == EINTR || errno == EAGAIN)) {
> - stats_api->stats_increment_value (conn_info->stats_handle, "sem_retry_count");
> + api->stats_increment_value (conn_info->stats_handle, "sem_retry_count");
> goto retry_semop;
> } else
> if ((res == -1) && (errno == EINVAL || errno == EIDRM)) {
> return (0);
> }
> #endif
> - stats_api->stats_increment_value (conn_info->stats_handle, "responses");
> + api->stats_increment_value (conn_info->stats_handle, "responses");
> return (0);
> }
>
> @@ -1103,14 +1149,14 @@
> retry_semop:
> res = semop (conn_info->semid, &sop, 1);
> if ((res == -1) && (errno == EINTR || errno == EAGAIN)) {
> - stats_api->stats_increment_value (conn_info->stats_handle, "sem_retry_count");
> + api->stats_increment_value (conn_info->stats_handle, "sem_retry_count");
> goto retry_semop;
> } else
> if ((res == -1) && (errno == EINVAL || errno == EIDRM)) {
> return (0);
> }
> #endif
> - stats_api->stats_increment_value (conn_info->stats_handle, "responses");
> + api->stats_increment_value (conn_info->stats_handle, "responses");
> return (0);
> }
>
> @@ -1182,14 +1228,14 @@
> retry_semop:
> res = semop (conn_info->semid, &sop, 1);
> if ((res == -1) && (errno == EINTR || errno == EAGAIN)) {
> - stats_api->stats_increment_value (conn_info->stats_handle, "sem_retry_count");
> + api->stats_increment_value (conn_info->stats_handle, "sem_retry_count");
> goto retry_semop;
> } else
> if ((res == -1) && (errno == EINVAL || errno == EIDRM)) {
> return;
> }
> #endif
> - stats_api->stats_increment_value (conn_info->stats_handle, "dispatched");
> + api->stats_increment_value (conn_info->stats_handle, "dispatched");
> }
>
> static void outq_flush (struct conn_info *conn_info) {
> @@ -1242,11 +1288,11 @@
> sizeof (mar_req_priv_change),
> MSG_NOSIGNAL);
> if (res == -1 && errno == EINTR) {
> - stats_api->stats_increment_value (conn_info->stats_handle, "recv_retry_count");
> + api->stats_increment_value (conn_info->stats_handle, "recv_retry_count");
> goto retry_recv;
> }
> if (res == -1 && errno == EAGAIN) {
> - stats_api->stats_increment_value (conn_info->stats_handle, "recv_retry_count");
> + api->stats_increment_value (conn_info->stats_handle, "recv_retry_count");
> goto retry_recv;
> }
> if (res == -1 && errno != EAGAIN) {
> @@ -1387,13 +1433,16 @@
> }
>
> if (new_fd == -1) {
> - api->log_printf ("Could not accept Library connection: %s\n", strerror (errno));
> + log_printf (LOGSYS_LEVEL_ERROR,
> + "Could not accept Library connection: %s\n", strerror (errno));
> return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
> }
>
> res = fcntl (new_fd, F_SETFL, O_NONBLOCK);
> if (res == -1) {
> - api->log_printf ("Could not set non-blocking operation on library connection: %s\n", strerror (errno));
> + log_printf (LOGSYS_LEVEL_ERROR,
> + "Could not set non-blocking operation on library connection: %s\n",
> + strerror (errno));
> close (new_fd);
> return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
> }
> @@ -1475,8 +1524,8 @@
> } else
> snprintf (conn_name, sizeof(conn_name), "%d", conn->fd);
>
> - conn->stats_handle = stats_api->stats_create_connection (conn_name, conn->client_pid, conn->fd);
> - stats_api->stats_update_value (conn->stats_handle, "service_id",
> + conn->stats_handle = api->stats_create_connection (conn_name, conn->client_pid, conn->fd);
> + api->stats_update_value (conn->stats_handle, "service_id",
> &conn->service, sizeof(conn->service));
> }
>
> Index: exec/main.c
> ===================================================================
> --- exec/main.c (revision 2529)
> +++ exec/main.c (working copy)
> @@ -99,6 +99,8 @@
>
> #define SERVER_BACKLOG 5
>
> +/* #define USE_IPC_API_V1 1 */
> +
> static int sched_priority = 0;
>
> static unsigned int service_count = 32;
> @@ -873,6 +875,7 @@
>
> static int ipc_subsys_id = -1;
>
> +#ifdef USE_IPC_API_V1
> static void ipc_log_printf (const char *format, ...) __attribute__((format(printf, 1, 2)));
> static void ipc_log_printf (const char *format, ...) {
> va_list ap;
> @@ -888,6 +891,7 @@
>
> va_end (ap);
> }
> +#endif /* USE_IPC_API_V1 */
>
> static void ipc_fatal_error(const char *error_msg) {
> _logsys_log_printf (
> @@ -1035,6 +1039,7 @@
> &key_incr_dummy);
> }
>
> +#ifdef USE_IPC_API_V1
> static struct coroipcs_init_state ipc_init_state = {
> .socket_name = COROSYNC_SOCKET_NAME,
> .sched_policy = SCHED_OTHER,
> @@ -1057,12 +1062,34 @@
> .exit_fn_get = corosync_exit_fn_get,
> .handler_fn_get = corosync_handler_fn_get
> };
> +#endif /* USE_IPC_API_V1 */
>
> -static struct coroipcs_init_stats_state ipc_init_stats_state = {
> - .stats_create_connection = corosync_stats_create_connection,
> - .stats_destroy_connection = corosync_stats_destroy_connection,
> - .stats_update_value = corosync_stats_update_value,
> - .stats_increment_value = corosync_stats_increment_value
> +
> +static struct coroipcs_init_state_v2 ipc_init_state_v2 = {
> + .socket_name = COROSYNC_SOCKET_NAME,
> + .sched_policy = SCHED_OTHER,
> + .sched_param = &global_sched_param,
> + .malloc = malloc,
> + .free = free,
> + .log_printf = _logsys_log_printf,
> + .fatal_error = ipc_fatal_error,
> + .security_valid = corosync_security_valid,
> + .service_available = corosync_service_available,
> + .private_data_size_get = corosync_private_data_size_get,
> + .serialize_lock = serialize_lock,
> + .serialize_unlock = serialize_unlock,
> + .sending_allowed = corosync_sending_allowed,
> + .sending_allowed_release = corosync_sending_allowed_release,
> + .poll_accept_add = corosync_poll_accept_add,
> + .poll_dispatch_add = corosync_poll_dispatch_add,
> + .poll_dispatch_modify = corosync_poll_dispatch_modify,
> + .init_fn_get = corosync_init_fn_get,
> + .exit_fn_get = corosync_exit_fn_get,
> + .handler_fn_get = corosync_handler_fn_get,
> + .stats_create_connection = corosync_stats_create_connection,
> + .stats_destroy_connection = corosync_stats_destroy_connection,
> + .stats_update_value = corosync_stats_update_value,
> + .stats_increment_value = corosync_stats_increment_value,
> };
>
> static void corosync_setscheduler (void)
> @@ -1084,7 +1111,10 @@
> /*
> * Turn on SCHED_RR in ipc system
> */
> +#ifdef USE_IPC_API_V1
> ipc_init_state.sched_policy = SCHED_RR;
> +#endif /* USE_IPC_API_V1 */
> + ipc_init_state_v2.sched_policy = SCHED_RR;
>
> /*
> * Turn on SCHED_RR in logsys system
> @@ -1461,9 +1491,9 @@
> */
> priv_drop ();
>
> - schedwrk_init (
> - serialize_lock,
> - serialize_unlock);
> + schedwrk_init (
> + serialize_lock,
> + serialize_unlock);
>
> ipc_subsys_id = _logsys_subsys_create ("IPC");
> if (ipc_subsys_id < 0) {
> @@ -1472,8 +1502,12 @@
> corosync_exit_error (AIS_DONE_INIT_SERVICES);
> }
>
> +#ifdef USE_IPC_API_V1
> coroipcs_ipc_init (&ipc_init_state);
> - coroipcs_ipc_stats_init (&ipc_init_stats_state);
> +#else
> + ipc_init_state_v2.log_subsys_id = ipc_subsys_id;
> + coroipcs_ipc_init_v2 (&ipc_init_state_v2);
> +#endif /* USE_IPC_API_V1 */
>
> /*
> * Start main processing loop
>
>
>
>
>
>
>
> NOTICE: This message contains privileged and confidential
> information intended only for the use of the addressee
> named above. If you are not the intended recipient of
> this message you are hereby notified that you must not
> disseminate, copy or take any action in reliance on it.
> If you have received this message in error please
> notify Allied Telesis Labs Ltd immediately.
> Any views expressed in this message are those of the
> individual sender, except where the sender has the
> authority to issue and specifically states them to
> be the views of Allied Telesis Labs.
More information about the Openais
mailing list