Changed client <-> server interface to make less system calls.

1998-06-18  Martin Baulig  <baulig@taurus.uni-trier.de>

	* lib/{command, write, read}.c: Changed client <-> server
	interface to make less system calls.

	* src/daemon/main.c: Changed server side of interface.

	* include/glibtop/command.h (struct _glibtop_response): New
	structure to return data from the server to the client.
This commit is contained in:
Martin Baulig
1998-06-18 10:22:39 +00:00
committed by Martin Baulig
parent d9e6288b7a
commit 2de9ea5c7d
8 changed files with 203 additions and 235 deletions

View File

@@ -1,3 +1,13 @@
1998-06-18 Martin Baulig <baulig@taurus.uni-trier.de>
* lib/{command, write, read}.c: Changed client <-> server
interface to make less system calls.
* src/daemon/main.c: Changed server side of interface.
* include/glibtop/command.h (struct _glibtop_response): New
structure to return data from the server to the client.
1998-06-14 Martin Baulig <martin@home-of-linux.org> 1998-06-14 Martin Baulig <martin@home-of-linux.org>
* glibtop.h (struct _glibtop): Added `socket' field. * glibtop.h (struct _glibtop): Added `socket' field.

View File

@@ -50,13 +50,29 @@ __BEGIN_DECLS
#define GLIBTOP_MAX_CMND 18 #define GLIBTOP_MAX_CMND 18
#define _GLIBTOP_PARAM_SIZE 16
typedef struct _glibtop_command glibtop_command; typedef struct _glibtop_command glibtop_command;
typedef struct _glibtop_response glibtop_response;
struct _glibtop_command struct _glibtop_command
{ {
glibtop server; glibtop server;
unsigned command; unsigned command;
size_t size; size_t size, data_size;
char parameter [_GLIBTOP_PARAM_SIZE];
};
union _glibtop_response_union
{
glibtop_union data;
glibtop_sysdeps sysdeps;
};
struct _glibtop_response
{
size_t data_size;
union _glibtop_response_union u;
}; };
#define glibtop_call(p1, p2, p3, p4) glibtop_call_r(glibtop_global_server, p1, p2, p3, p4) #define glibtop_call(p1, p2, p3, p4) glibtop_call_r(glibtop_global_server, p1, p2, p3, p4)

View File

@@ -4,7 +4,7 @@ INCLUDES = -I$(top_builddir) -I$(top_srcdir) @machine_incs@ \
-I$(top_srcdir)/include -I$(top_srcdir)/intl @GUILE_INCS@ \ -I$(top_srcdir)/include -I$(top_srcdir)/intl @GUILE_INCS@ \
-DGTOPLOCALEDIR=\"$(datadir)/locale\" -D_GNU_SOURCE -DGTOPLOCALEDIR=\"$(datadir)/locale\" -D_GNU_SOURCE
CFLAGS = -Wall -W @CFLAGS@ -DGTOP_SERVER=\""@LIBGTOP_SERVER@"\" CFLAGS = -Wall -W @CFLAGS@ -DGTOP_SERVER=\""@LIBGTOP_SERVER@"\" -DDEBUG
lib_LTLIBRARIES = libgtop.la lib_LTLIBRARIES = libgtop.la

View File

@@ -30,25 +30,41 @@ void *
glibtop_call_l (glibtop *server, unsigned command, size_t send_size, void *send_buf, glibtop_call_l (glibtop *server, unsigned command, size_t send_size, void *send_buf,
size_t recv_size, void *recv_buf) size_t recv_size, void *recv_buf)
{ {
glibtop_command *cmnd; glibtop_command cmnd;
glibtop_response response;
void *ptr; void *ptr;
glibtop_init_r (&server, 0, 0); glibtop_init_r (&server, 0, 0);
cmnd = glibtop_calloc_r (server, 1, sizeof (glibtop_command)); memset (&cmnd, 0, sizeof (glibtop_command));
memcpy (&cmnd->server, server, sizeof (glibtop)); memcpy (&cmnd.server, server, sizeof (glibtop));
cmnd->command = command; cmnd.command = command;
cmnd->size = send_size;
glibtop_write_l (server, sizeof (glibtop_command), cmnd); /* If send_size is less than _GLIBTOP_PARAM_SIZE (normally 16 Bytes), we
* send it together with command, so we only need one system call instead
* of two. */
#ifdef DEBUG
fprintf (stderr, "COMMAND: send_size = %d; command = %d; sizeof (cmnd) = %d\n",
send_size, command, sizeof (glibtop_command));
#endif
if (send_size <= _GLIBTOP_PARAM_SIZE)
memcpy (cmnd.parameter, send_buf, send_size);
else
cmnd.size = send_size;
glibtop_write_l (server, sizeof (glibtop_command), &cmnd);
glibtop_write_l (server, send_size, send_buf); glibtop_write_l (server, send_size, send_buf);
glibtop_read_l (server, recv_size, recv_buf);
ptr = glibtop_read_data_l (server); glibtop_read_l (server, sizeof (glibtop_response), &response);
glibtop_free_r (server, cmnd); /* glibtop_read_l (server, recv_size, recv_buf); */
return ptr; /* ptr = glibtop_read_data_l (server); */
return NULL;
} }

View File

@@ -26,27 +26,10 @@
void void
glibtop_read_l (glibtop *server, size_t size, void *buf) glibtop_read_l (glibtop *server, size_t size, void *buf)
{ {
size_t ssize;
int ret; int ret;
glibtop_init_r (&server, 0, 0); glibtop_init_r (&server, 0, 0);
#ifdef DEBUG
fprintf (stderr, "LIBRARY: reading %d bytes, should be %d.\n", sizeof (size_t), size);
#endif
if (server->socket) {
ret = recv (server->socket, &ssize, sizeof (size_t), 0);
} else {
ret = read (server->input [0], &ssize, sizeof (size_t));
}
if (ret < 0)
glibtop_error_io_r (server, _("read size"));
if (size != ssize)
glibtop_error_r (server, _("got %d bytes but requested %d (ret = %d)"), ssize, size, ret);
#ifdef DEBUG #ifdef DEBUG
fprintf (stderr, "LIBRARY: really reading %d bytes.\n", size); fprintf (stderr, "LIBRARY: really reading %d bytes.\n", size);
#endif #endif

View File

@@ -30,20 +30,7 @@ glibtop_write_l (glibtop *server, size_t size, void *buf)
glibtop_init_r (&server, 0, 0); glibtop_init_r (&server, 0, 0);
#ifdef DEBUG if (size == 0) return;
fprintf (stderr, "LIBRARY: writing %d bytes = %d.\n", sizeof (size_t), size);
#endif
if (server->socket) {
ret = send (server->socket, &size, sizeof (size_t), 0);
} else {
ret = write (server->output [1], &size, sizeof (size_t));
}
if (ret < 0)
glibtop_error_io_r (server, _("write size"));
if (!size) return;
#ifdef DEBUG #ifdef DEBUG
fprintf (stderr, "LIBRARY: really writing %d bytes.\n", size); fprintf (stderr, "LIBRARY: really writing %d bytes.\n", size);

View File

@@ -4,7 +4,7 @@ INCLUDES = -I$(top_builddir) -I$(top_srcdir) @machine_incs@ \
-I$(top_srcdir)/include -I$(top_srcdir)/intl @GUILE_INCS@ \ -I$(top_srcdir)/include -I$(top_srcdir)/intl @GUILE_INCS@ \
-DGTOPLOCALEDIR=\"$(datadir)/locale\" -D_GNU_SOURCE -DGTOPLOCALEDIR=\"$(datadir)/locale\" -D_GNU_SOURCE
CFLAGS = -Wall -W @CFLAGS@ -DGTOP_SERVER=\""@LIBGTOP_SERVER@"\" CFLAGS = -Wall -W @CFLAGS@ -DGTOP_SERVER=\""@LIBGTOP_SERVER@"\" -DDEBUG -DREAL_DEBUG
bin_PROGRAMS = gnuserv bin_PROGRAMS = gnuserv

View File

@@ -39,24 +39,25 @@
#endif #endif
static void static void
do_output (int s, size_t size, const void *buf) do_output (int s, glibtop_response *response, size_t data_size, const void *data)
{ {
#ifdef REAL_DEBUG #ifdef REAL_DEBUG
fprintf (stderr, "Writing %d bytes = %d.\n", sizeof (size_t), size); fprintf (stderr, "Really writing %d bytes.\n", sizeof (glibtop_response));
#endif #endif
if (send (s, &size, sizeof (size_t), 0) < 0) response->data_size = data_size;
if (send (s, response, sizeof (glibtop_response), 0) < 0)
glibtop_warn_io ("send"); glibtop_warn_io ("send");
if (!size) if (response->data_size) {
return;
#ifdef REAL_DEBUG #ifdef REAL_DEBUG
fprintf (stderr, "Really writing %d bytes.\n", size); fprintf (stderr, "Writing %d bytes of data.\n", response->data_size);
#endif #endif
if (send (s, buf, size, 0) < 0) if (send (s, data, response->data_size, 0) , 0)
glibtop_warn_io ("send"); glibtop_warn_io ("send");
}
} }
void void
@@ -64,12 +65,11 @@ handle_socket_connection (int s)
{ {
pid_t pid; pid_t pid;
int nread; int nread;
size_t size, dsize; size_t size;
char parameter [BUFSIZ]; char parameter [BUFSIZ];
struct timeval tv; struct timeval tv;
glibtop_union data; glibtop_response response;
glibtop_command cmnd; glibtop_command cmnd;
glibtop_sysdeps sysdeps;
glibtop server; glibtop server;
void *ptr; void *ptr;
@@ -77,7 +77,7 @@ handle_socket_connection (int s)
tv.tv_usec = 0; tv.tv_usec = 0;
while(1) { while(1) {
nread = recv (s, &size, sizeof (size_t), 0); nread = recv (s, &cmnd, sizeof (glibtop_command), 0);
if (nread < 0) { if (nread < 0) {
glibtop_warn_io ("recv"); glibtop_warn_io ("recv");
@@ -87,55 +87,27 @@ handle_socket_connection (int s)
if (nread == 0) if (nread == 0)
return; return;
if (nread != sizeof (size_t)) { if (nread != sizeof (glibtop_command))
glibtop_warn ("Expected %d bytes but got %d", sizeof (size_t), nread); glibtop_warn ("Received %d bytes, but expected %d\n",
return; nread, sizeof (glibtop_command));
}
if (size != sizeof (glibtop_command)) {
glibtop_warn ("Expected %d bytes but got %d", sizeof (size_t), nread);
return;
}
nread = recv (s, &cmnd, size, 0);
if (nread < 0) {
glibtop_warn_io ("recv");
return;
}
if (nread == 0)
return;
#ifdef REAL_DEBUG #ifdef REAL_DEBUG
fprintf (stderr, "Received command %d from client.\n", cmnd.command); fprintf (stderr, "Received command %d from client.\n", cmnd.command);
#endif #endif
nread = recv (s, &dsize, sizeof (size_t), 0); if (cmnd.size >= BUFSIZ) {
glibtop_warn ("Client sent %d bytes, but buffer is %d", cmnd.size, BUFSIZ);
/* will return 0 if parent exits. */
if (nread < 0) {
glibtop_warn_io ("recv");
return;
}
if (nread == 0)
return;
if (dsize >= BUFSIZ) {
glibtop_warn ("Client sent %d bytes, but buffer is %d", dsize, BUFSIZ);
return; return;
} }
memset (parameter, 0, sizeof (parameter)); memset (parameter, 0, sizeof (parameter));
if (dsize) { if (cmnd.size) {
#ifdef REAL_DEBUG #ifdef REAL_DEBUG
fprintf (stderr, "Client has %d bytes of data.\n", dsize); fprintf (stderr, "Client has %d bytes of data.\n", cmnd.size);
#endif #endif
nread = recv (s, parameter, dsize, 0); nread = recv (s, parameter, cmnd.size, 0);
/* will return 0 if parent exits. */ /* will return 0 if parent exits. */
@@ -147,112 +119,96 @@ handle_socket_connection (int s)
if (nread == 0) if (nread == 0)
return; return;
if (nread != (int) dsize) { if (nread != (int) cmnd.size) {
glibtop_warn ("Expected %d bytes but got %d", dsize, nread); glibtop_warn ("Expected %d bytes but got %d",
cmnd.size, nread);
return; return;
} }
} }
switch (cmnd.command) { switch (cmnd.command) {
case GLIBTOP_CMND_SYSDEPS: case GLIBTOP_CMND_SYSDEPS:
sysdeps.features = GLIBTOP_SYSDEPS_ALL; response.u.sysdeps.features = GLIBTOP_SYSDEPS_ALL;
do_output (s, sizeof (glibtop_sysdeps), &sysdeps); do_output (s, &response, 0, NULL);
do_output (s, 0, NULL);
break; break;
case GLIBTOP_CMND_CPU: case GLIBTOP_CMND_CPU:
glibtop_get_cpu_l (&server, &data.cpu); glibtop_get_cpu_l (&server, &response.u.data.cpu);
do_output (s, sizeof (glibtop_cpu), &data.cpu); do_output (s, &response, 0, NULL);
do_output (s, 0, NULL);
break; break;
case GLIBTOP_CMND_MEM: case GLIBTOP_CMND_MEM:
glibtop_get_mem_l (&server, &data.mem); glibtop_get_mem_l (&server, &response.u.data.mem);
do_output (s, sizeof (glibtop_mem), &data.mem); do_output (s, &response, 0, NULL);
do_output (s, 0, NULL);
break; break;
case GLIBTOP_CMND_SWAP: case GLIBTOP_CMND_SWAP:
glibtop_get_swap_l (&server, &data.swap); glibtop_get_swap_l (&server, &response.u.data.swap);
do_output (s, sizeof (glibtop_swap), &data.swap); do_output (s, &response, 0, NULL);
do_output (s, 0, NULL);
break; break;
case GLIBTOP_CMND_UPTIME: case GLIBTOP_CMND_UPTIME:
glibtop_get_uptime_l (&server, &data.uptime); glibtop_get_uptime_l (&server, &response.u.data.uptime);
do_output (s, sizeof (glibtop_uptime), &data.uptime); do_output (s, &response, 0, NULL);
do_output (s, 0, NULL);
break; break;
case GLIBTOP_CMND_LOADAVG: case GLIBTOP_CMND_LOADAVG:
glibtop_get_loadavg_l (&server, &data.loadavg); glibtop_get_loadavg_l (&server, &response.u.data.loadavg);
do_output (s, sizeof (glibtop_loadavg), &data.loadavg); do_output (s, &response, 0, NULL);
do_output (s, 0, NULL);
break; break;
case GLIBTOP_CMND_SHM_LIMITS: case GLIBTOP_CMND_SHM_LIMITS:
glibtop_get_shm_limits_l (&server, &data.shm_limits); glibtop_get_shm_limits_l (&server, &response.u.data.shm_limits);
do_output (s, sizeof (glibtop_shm_limits), &data.shm_limits); do_output (s, &response, 0, NULL);
do_output (s, 0, NULL);
break; break;
case GLIBTOP_CMND_MSG_LIMITS: case GLIBTOP_CMND_MSG_LIMITS:
glibtop_get_msg_limits_l (&server, &data.msg_limits); glibtop_get_msg_limits_l (&server, &response.u.data.msg_limits);
do_output (s, sizeof (glibtop_msg_limits), &data.msg_limits); do_output (s, &response, 0, NULL);
do_output (s, 0, NULL);
break; break;
case GLIBTOP_CMND_SEM_LIMITS: case GLIBTOP_CMND_SEM_LIMITS:
glibtop_get_sem_limits_l (&server, &data.sem_limits); glibtop_get_sem_limits_l (&server, &response.u.data.sem_limits);
do_output (s, sizeof (glibtop_sem_limits), &data.sem_limits); do_output (s, &response, 0, NULL);
do_output (s, 0, NULL);
break; break;
case GLIBTOP_CMND_PROCLIST: case GLIBTOP_CMND_PROCLIST:
ptr = glibtop_get_proclist_l (&server, &data.proclist); ptr = glibtop_get_proclist_l (&server, &response.u.data.proclist);
do_output (s, sizeof (glibtop_proclist), &data.proclist); do_output (s, &response, response.u.data.proclist.total, ptr);
do_output (s, data.proclist.total, ptr);
glibtop_free_r (&server, ptr); glibtop_free_r (&server, ptr);
break; break;
case GLIBTOP_CMND_PROC_STATE: case GLIBTOP_CMND_PROC_STATE:
memcpy (&pid, parameter, sizeof (pid_t)); memcpy (&pid, parameter, sizeof (pid_t));
glibtop_get_proc_state_l glibtop_get_proc_state_l
(&server, &data.proc_state, pid); (&server, &response.u.data.proc_state, pid);
do_output (s, sizeof (glibtop_proc_state), &data.proc_state); do_output (s, &response, 0, NULL);
do_output (s, 0, NULL);
break; break;
case GLIBTOP_CMND_PROC_UID: case GLIBTOP_CMND_PROC_UID:
memcpy (&pid, parameter, sizeof (pid_t)); memcpy (&pid, parameter, sizeof (pid_t));
glibtop_get_proc_uid_l glibtop_get_proc_uid_l
(&server, &data.proc_uid, pid); (&server, &response.u.data.proc_uid, pid);
do_output (s, sizeof (glibtop_proc_uid), &data.proc_uid); do_output (s, &response, 0, NULL);
do_output (s, 0, NULL);
break; break;
case GLIBTOP_CMND_PROC_MEM: case GLIBTOP_CMND_PROC_MEM:
memcpy (&pid, parameter, sizeof (pid_t)); memcpy (&pid, parameter, sizeof (pid_t));
glibtop_get_proc_mem_l glibtop_get_proc_mem_l
(&server, &data.proc_mem, pid); (&server, &response.u.data.proc_mem, pid);
do_output (s, sizeof (glibtop_proc_mem), &data.proc_mem); do_output (s, &response, 0, NULL);
do_output (s, 0, NULL);
break; break;
case GLIBTOP_CMND_PROC_TIME: case GLIBTOP_CMND_PROC_TIME:
memcpy (&pid, parameter, sizeof (pid_t)); memcpy (&pid, parameter, sizeof (pid_t));
glibtop_get_proc_time_l glibtop_get_proc_time_l
(&server, &data.proc_time, pid); (&server, &response.u.data.proc_time, pid);
do_output (s, sizeof (glibtop_proc_time), &data.proc_time); do_output (s, &response, 0, NULL);
do_output (s, 0, NULL);
break; break;
case GLIBTOP_CMND_PROC_SIGNAL: case GLIBTOP_CMND_PROC_SIGNAL:
memcpy (&pid, parameter, sizeof (pid_t)); memcpy (&pid, parameter, sizeof (pid_t));
glibtop_get_proc_signal_l glibtop_get_proc_signal_l
(&server, &data.proc_signal, pid); (&server, &response.u.data.proc_signal, pid);
do_output (s, sizeof (glibtop_proc_signal), &data.proc_signal); do_output (s, &response, 0, NULL);
do_output (s, 0, NULL);
break; break;
case GLIBTOP_CMND_PROC_KERNEL: case GLIBTOP_CMND_PROC_KERNEL:
memcpy (&pid, parameter, sizeof (pid_t)); memcpy (&pid, parameter, sizeof (pid_t));
glibtop_get_proc_kernel_l glibtop_get_proc_kernel_l
(&server, &data.proc_kernel, pid); (&server, &response.u.data.proc_kernel, pid);
do_output (s, sizeof (glibtop_proc_kernel), &data.proc_kernel); do_output (s, &response, 0, NULL);
do_output (s, 0, NULL);
break; break;
case GLIBTOP_CMND_PROC_SEGMENT: case GLIBTOP_CMND_PROC_SEGMENT:
memcpy (&pid, parameter, sizeof (pid_t)); memcpy (&pid, parameter, sizeof (pid_t));
glibtop_get_proc_segment_l glibtop_get_proc_segment_l
(&server, &data.proc_segment, pid); (&server, &response.u.data.proc_segment, pid);
do_output (s, sizeof (glibtop_proc_segment), &data.proc_segment); do_output (s, &response, 0, NULL);
do_output (s, 0, NULL);
break; break;
} }
} }