* [PATCH 1/3] ipcs: add --human readable size conversion option
@ 2012-12-09 21:08 Sami Kerola
2012-12-09 21:08 ` [PATCH 2/3] docs: swapon.8 option name fix Sami Kerola
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Sami Kerola @ 2012-12-09 21:08 UTC (permalink / raw)
To: util-linux; +Cc: kerolasa
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
sys-utils/ipcs.1 | 3 ++
sys-utils/ipcs.c | 155 ++++++++++++++++++++++++++++++++++++++++++-------------
2 files changed, 121 insertions(+), 37 deletions(-)
diff --git a/sys-utils/ipcs.1 b/sys-utils/ipcs.1
index 7c094c8..3c6b8c3 100644
--- a/sys-utils/ipcs.1
+++ b/sys-utils/ipcs.1
@@ -65,6 +65,9 @@ Show resource limits.
.TP
\fB\-u\fR, \fB\-\-summary\fR
Show status summary.
+.TP
+.B \-\-human
+Show print sizes in human readable format (e.g., 1K 234M 2G).
.SH SEE ALSO
.BR ipcrm (1),
.BR ipcmk (1),
diff --git a/sys-utils/ipcs.c b/sys-utils/ipcs.c
index 6ef0bbe..9fe24ac 100644
--- a/sys-utils/ipcs.c
+++ b/sys-utils/ipcs.c
@@ -22,6 +22,7 @@
#include "c.h"
#include "nls.h"
#include "closestream.h"
+#include "strutils.h"
#include "ipcutils.h"
@@ -31,13 +32,21 @@
#define TIME 4
#define PID 5
-static void do_shm (char format);
-static void print_shm (int id);
+static void do_shm (char format, int unit);
+static void print_shm (int id, int unit);
static void do_sem (char format);
static void print_sem (int id);
-static void do_msg (char format);
+static void do_msg (char format, int unit);
+static void print_msg (int id, int unit);
-void print_msg (int id);
+enum {
+ OPT_HUMAN = CHAR_MAX + 1
+};
+
+enum {
+ UNIT_DEFAULT,
+ UNIT_HUMAN
+};
static void __attribute__ ((__noreturn__)) usage(FILE * out)
{
@@ -61,6 +70,7 @@ static void __attribute__ ((__noreturn__)) usage(FILE * out)
fputs(_(" -c, --creator show creator and owner\n"), out);
fputs(_(" -l, --limits show resource limits\n"), out);
fputs(_(" -u, --summary show status summary\n"), out);
+ fputs(_(" --human show sizes in human readable format\n"), out);
fprintf(out, USAGE_MAN_TAIL("ipcs(1)"));
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
}
@@ -69,6 +79,7 @@ int main (int argc, char **argv)
{
int opt, msg = 0, sem = 0, shm = 0, id=0, print=0;
char format = 0;
+ int unit = UNIT_DEFAULT;
static const struct option longopts[] = {
{"id", required_argument, NULL, 'i'},
{"shmems", no_argument, NULL, 'm'},
@@ -80,6 +91,7 @@ int main (int argc, char **argv)
{"creator", no_argument, NULL, 'c'},
{"limits", no_argument, NULL, 'l'},
{"summary", no_argument, NULL, 'u'},
+ {"human", no_argument, NULL, OPT_HUMAN},
{"version", no_argument, NULL, 'V'},
{"help", no_argument, NULL, 'h'},
{NULL, 0, NULL, 0}
@@ -124,6 +136,9 @@ int main (int argc, char **argv)
case 'u':
format = STATUS;
break;
+ case OPT_HUMAN:
+ unit = UNIT_HUMAN;
+ break;
case 'h':
usage(stdout);
case 'V':
@@ -136,11 +151,11 @@ int main (int argc, char **argv)
if (print) {
if (shm)
- print_shm (id);
+ print_shm (id, unit);
if (sem)
print_sem (id);
if (msg)
- print_msg (id);
+ print_msg (id, unit);
if (!shm && !sem && !msg )
usage (stderr);
} else {
@@ -149,7 +164,7 @@ int main (int argc, char **argv)
printf ("\n");
if (shm) {
- do_shm (format);
+ do_shm (format, unit);
printf ("\n");
}
if (sem) {
@@ -157,14 +172,14 @@ int main (int argc, char **argv)
printf ("\n");
}
if (msg) {
- do_msg (format);
+ do_msg (format, unit);
printf ("\n");
}
}
return EXIT_SUCCESS;
}
-static void do_shm (char format)
+static void do_shm (char format, int unit)
{
struct passwd *pw;
struct shm_data *shmds, *shmdsp;
@@ -177,11 +192,28 @@ static void do_shm (char format)
printf (_("------ Shared Memory Limits --------\n"));
if (ipc_shm_get_limits(&lim))
return;
- printf (_("max number of segments = %ju\n"), lim.shmmni);
- printf (_("max seg size (kbytes) = %ju\n"), lim.shmmax / 1024);
- printf (_("max total shared memory (kbytes) = %ju\n"),
- (lim.shmall / 1024) * getpagesize());
- printf (_("min seg size (bytes) = %ju\n"), lim.shmmin);
+ printf(_("max number of segments = %ju\n"), lim.shmmni);
+ if (unit == UNIT_DEFAULT)
+ printf(_("max seg size (kbytes) = %ju\n"),
+ lim.shmmax / 1024);
+ else if (unit == UNIT_HUMAN)
+ printf(_("max seg size = %s\n"),
+ size_to_human_string(SIZE_SUFFIX_1LETTER,
+ lim.shmmax));
+ if (unit == UNIT_DEFAULT)
+ printf(_("max total shared memory (kbytes) = %ju\n"),
+ (lim.shmall / 1024) * getpagesize());
+ else if (unit == UNIT_HUMAN)
+ printf(_("max total shared memory = %s\n"),
+ size_to_human_string(SIZE_SUFFIX_1LETTER,
+ lim.shmall *
+ getpagesize()));
+ if (unit == UNIT_DEFAULT)
+ printf(_("min seg size (bytes) = %ju\n"), lim.shmmin);
+ else if (unit == UNIT_HUMAN)
+ printf(_("min seg size = %s\n"),
+ size_to_human_string(SIZE_SUFFIX_1LETTER,
+ lim.shmmin));
return;
}
case STATUS:
@@ -245,7 +277,8 @@ static void do_shm (char format)
default:
printf (_("------ Shared Memory Segments --------\n"));
printf ("%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n",
- _("key"),_("shmid"),_("owner"),_("perms"),_("bytes"),
+ _("key"),_("shmid"),_("owner"),_("perms"),
+ unit == UNIT_HUMAN ? _("size") : _("bytes"),
_("nattch"),_("status"));
break;
}
@@ -292,12 +325,18 @@ static void do_shm (char format)
printf ("%-10d %-10.10s", shmdsp->shm_perm.id, pw->pw_name);
else
printf ("%-10d %-10u", shmdsp->shm_perm.id, shmdsp->shm_perm.uid);
- printf (" %-10o %-10lu %-10ld %-6s %-6s\n",
- shmdsp->shm_perm.mode & 0777,
- shmdsp->shm_segsz,
- shmdsp->shm_nattch,
- shmdsp->shm_perm.mode & SHM_DEST ? _("dest") : " ",
- shmdsp->shm_perm.mode & SHM_LOCKED ? _("locked") : " ");
+ printf(" %-10o", shmdsp->shm_perm.mode & 0777);
+ if (unit == UNIT_DEFAULT)
+ printf(" %-10lu", shmdsp->shm_segsz);
+ else if (unit == UNIT_HUMAN)
+ printf(" %-10s",
+ size_to_human_string(SIZE_SUFFIX_1LETTER,
+ shmdsp->shm_segsz));
+ printf(" %-10ld %-6s %-6s\n", shmdsp->shm_nattch,
+ shmdsp->shm_perm.
+ mode & SHM_DEST ? _("dest") : " ",
+ shmdsp->shm_perm.
+ mode & SHM_LOCKED ? _("locked") : " ");
break;
}
}
@@ -407,7 +446,7 @@ static void do_sem (char format)
return;
}
-static void do_msg (char format)
+static void do_msg (char format, int unit)
{
struct passwd *pw;
struct msg_data *msgds, *msgdsp;
@@ -421,8 +460,20 @@ static void do_msg (char format)
return;
printf (_("------ Messages Limits --------\n"));
printf (_("max queues system wide = %d\n"), lim.msgmni);
- printf (_("max size of message (bytes) = %zu\n"), lim.msgmax);
- printf (_("default max size of queue (bytes) = %d\n"), lim.msgmnb);
+ if (unit == UNIT_DEFAULT)
+ printf(_("max size of message (bytes) = %zu\n"),
+ lim.msgmax);
+ else if (unit == UNIT_HUMAN)
+ printf(_("max size of message = %s\n"),
+ size_to_human_string(SIZE_SUFFIX_1LETTER,
+ lim.msgmax));
+ if (unit == UNIT_DEFAULT)
+ printf(_("default max size of queue (bytes) = %d\n"),
+ lim.msgmnb);
+ else if (unit == UNIT_HUMAN)
+ printf(_("default max size of queue = %s\n"),
+ size_to_human_string(SIZE_SUFFIX_1LETTER,
+ lim.msgmnb));
return;
}
case STATUS:
@@ -434,8 +485,18 @@ static void do_msg (char format)
}
printf (_("------ Messages Status --------\n"));
printf (_("allocated queues = %d\n"), msginfo.msgpool);
- printf (_("used headers = %d\n"), msginfo.msgmap);
- printf (_("used space = %d bytes\n"), msginfo.msgtql);
+ if (unit == UNIT_DEFAULT)
+ printf(_("used headers = %d\n"), msginfo.msgmap);
+ else if (unit == UNIT_HUMAN)
+ printf(_("used headers = %s\n"),
+ size_to_human_string(SIZE_SUFFIX_1LETTER,
+ msginfo.msgmap));
+ if (unit == UNIT_DEFAULT)
+ printf(_("used space = %d bytes\n"), msginfo.msgtql);
+ else if (unit == UNIT_HUMAN)
+ printf(_("used space = %s\n"),
+ size_to_human_string(SIZE_SUFFIX_1LETTER,
+ msginfo.msgtql));
return;
}
case CREATOR:
@@ -460,7 +521,8 @@ static void do_msg (char format)
printf (_("------ Message Queues --------\n"));
printf ("%-10s %-10s %-10s %-10s %-12s %-12s\n",
_("key"), _("msqid"), _("owner"), _("perms"),
- _("used-bytes"), _("messages"));
+ unit == UNIT_HUMAN ? _("size") : _("used-bytes"),
+ _("messages"));
break;
}
@@ -505,10 +567,14 @@ static void do_msg (char format)
printf ("%-10d %-10.10s", msgdsp->msg_perm.id, pw->pw_name);
else
printf ("%-10d %-10u", msgdsp->msg_perm.id, msgdsp->msg_perm.uid);
- printf (" %-10o %-12ld %-12ld\n",
- msgdsp->msg_perm.mode & 0777,
- msgdsp->q_cbytes,
- msgdsp->q_qnum);
+ printf(" %-10o", msgdsp->msg_perm.mode & 0777);
+ if (unit == UNIT_DEFAULT)
+ printf(" %-12ld", msgdsp->q_cbytes);
+ else if (unit == UNIT_HUMAN)
+ printf(" %-12s",
+ size_to_human_string(SIZE_SUFFIX_1LETTER,
+ msgdsp->q_cbytes));
+ printf(" %-12ld\n", msgdsp->q_qnum);
break;
}
}
@@ -517,7 +583,7 @@ static void do_msg (char format)
return;
}
-static void print_shm(int shmid)
+static void print_shm(int shmid, int unit)
{
struct shm_data *shmdata;
@@ -532,8 +598,14 @@ static void print_shm(int shmid)
shmdata->shm_perm.cuid, shmdata->shm_perm.cgid);
printf(_("mode=%#o\taccess_perms=%#o\n"), shmdata->shm_perm.mode,
shmdata->shm_perm.mode & 0777);
- printf(_("bytes=%ju\tlpid=%u\tcpid=%u\tnattch=%jd\n"),
- shmdata->shm_segsz, shmdata->shm_lprid, shmdata->shm_cprid,
+ if (unit == UNIT_DEFAULT)
+ printf(_("bytes=%ju\t"), shmdata->shm_segsz);
+ else if (unit == UNIT_HUMAN)
+ printf(_("size=%s\t"),
+ size_to_human_string(SIZE_SUFFIX_1LETTER,
+ shmdata->shm_segsz));
+ printf(_("lpid=%u\tcpid=%u\tnattch=%jd\n"),
+ shmdata->shm_lprid, shmdata->shm_cprid,
shmdata->shm_nattch);
printf(_("att_time=%-26.24s\n"),
shmdata->shm_atim ? ctime(&(shmdata->shm_atim)) : _("Not set"));
@@ -546,7 +618,7 @@ static void print_shm(int shmid)
}
-void print_msg(int msgid)
+void print_msg(int msgid, int unit)
{
struct msg_data *msgdata;
@@ -560,8 +632,17 @@ void print_msg(int msgid)
msgdata->msg_perm.uid, msgdata->msg_perm.uid,
msgdata->msg_perm.cuid, msgdata->msg_perm.cgid,
msgdata->msg_perm.mode);
- printf(_("cbytes=%jd\tqbytes=%jd\tqnum=%jd\tlspid=%d\tlrpid=%d\n"),
- msgdata->q_cbytes, msgdata->q_qbytes, msgdata->q_qnum,
+ if (unit == UNIT_DEFAULT)
+ printf(_("cbytes=%jd\tqbytes=%jd"), msgdata->q_cbytes,
+ msgdata->q_qbytes);
+ else if (unit == UNIT_HUMAN)
+ printf(_("csize=%s\tqsize=%s"),
+ size_to_human_string(SIZE_SUFFIX_1LETTER,
+ msgdata->q_cbytes),
+ size_to_human_string(SIZE_SUFFIX_1LETTER,
+ msgdata->q_qbytes));
+ printf(_("\tqnum=%jd\tlspid=%d\tlrpid=%d\n"),
+ msgdata->q_qnum,
msgdata->q_lspid, msgdata->q_lrpid);
printf(_("send_time=%-26.24s\n"),
msgdata->q_stime ? ctime(&msgdata->q_stime) : _("Not set"));
--
1.8.0.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH 2/3] docs: swapon.8 option name fix
2012-12-09 21:08 [PATCH 1/3] ipcs: add --human readable size conversion option Sami Kerola
@ 2012-12-09 21:08 ` Sami Kerola
2012-12-09 21:08 ` [PATCH 3/3] ipcs: add --bytes size output option Sami Kerola
2012-12-10 9:06 ` [PATCH 1/3] ipcs: add --human readable size conversion option Karel Zak
2 siblings, 0 replies; 5+ messages in thread
From: Sami Kerola @ 2012-12-09 21:08 UTC (permalink / raw)
To: util-linux; +Cc: kerolasa
The commit 7ae8b469adda598fda28e4ea647d47905b43e172 added option --bytes,
not the initial proposal which was changed after maillist discussion.
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
sys-utils/swapon.8 | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/sys-utils/swapon.8 b/sys-utils/swapon.8
index 2fbf855..be1f34a 100644
--- a/sys-utils/swapon.8
+++ b/sys-utils/swapon.8
@@ -182,7 +182,7 @@ Display
.B \-\-show
output without aligning table columns.
.TP
-.B \-\-inhuman
+.B \-\-bytes
Display swap size in bytes in
.B \-\-show
output instead of user friendly size and unit.
--
1.8.0.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH 3/3] ipcs: add --bytes size output option
2012-12-09 21:08 [PATCH 1/3] ipcs: add --human readable size conversion option Sami Kerola
2012-12-09 21:08 ` [PATCH 2/3] docs: swapon.8 option name fix Sami Kerola
@ 2012-12-09 21:08 ` Sami Kerola
2012-12-10 9:06 ` [PATCH 1/3] ipcs: add --human readable size conversion option Karel Zak
2 siblings, 0 replies; 5+ messages in thread
From: Sami Kerola @ 2012-12-09 21:08 UTC (permalink / raw)
To: util-linux; +Cc: kerolasa
This makes the command being a little closer standard compliant. See
IEEE Std 1003.1 referral link below for more information.
References: http://pubs.opengroup.org/onlinepubs/009696799/utilities/ipcs.html
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
sys-utils/ipcs.c | 34 +++++++++++++++++++++++-----------
1 file changed, 23 insertions(+), 11 deletions(-)
diff --git a/sys-utils/ipcs.c b/sys-utils/ipcs.c
index 9fe24ac..4b0c51c 100644
--- a/sys-utils/ipcs.c
+++ b/sys-utils/ipcs.c
@@ -45,7 +45,8 @@ enum {
enum {
UNIT_DEFAULT,
- UNIT_HUMAN
+ UNIT_HUMAN,
+ UNIT_BYTES
};
static void __attribute__ ((__noreturn__)) usage(FILE * out)
@@ -71,6 +72,7 @@ static void __attribute__ ((__noreturn__)) usage(FILE * out)
fputs(_(" -l, --limits show resource limits\n"), out);
fputs(_(" -u, --summary show status summary\n"), out);
fputs(_(" --human show sizes in human readable format\n"), out);
+ fputs(_(" -b, --bytes show sizes in bytes\n"), out);
fprintf(out, USAGE_MAN_TAIL("ipcs(1)"));
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
}
@@ -92,11 +94,12 @@ int main (int argc, char **argv)
{"limits", no_argument, NULL, 'l'},
{"summary", no_argument, NULL, 'u'},
{"human", no_argument, NULL, OPT_HUMAN},
+ {"bytes", no_argument, NULL, 'b'},
{"version", no_argument, NULL, 'V'},
{"help", no_argument, NULL, 'h'},
{NULL, 0, NULL, 0}
};
- char options[] = "i:mqsatpcluVh";
+ char options[] = "i:mqsatpclubVh";
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
@@ -139,6 +142,9 @@ int main (int argc, char **argv)
case OPT_HUMAN:
unit = UNIT_HUMAN;
break;
+ case 'b':
+ unit = UNIT_BYTES;
+ break;
case 'h':
usage(stdout);
case 'V':
@@ -200,6 +206,9 @@ static void do_shm (char format, int unit)
printf(_("max seg size = %s\n"),
size_to_human_string(SIZE_SUFFIX_1LETTER,
lim.shmmax));
+ else /* UNIT_BYTES */
+ printf(_("max seg size (bytes) = %ju\n"),
+ lim.shmmax);
if (unit == UNIT_DEFAULT)
printf(_("max total shared memory (kbytes) = %ju\n"),
(lim.shmall / 1024) * getpagesize());
@@ -208,7 +217,10 @@ static void do_shm (char format, int unit)
size_to_human_string(SIZE_SUFFIX_1LETTER,
lim.shmall *
getpagesize()));
- if (unit == UNIT_DEFAULT)
+ else /* UNIT_BYTES */
+ printf(_("max total shared memory (bytes) = %ju\n"),
+ lim.shmall * getpagesize());
+ if (unit == UNIT_DEFAULT || unit == UNIT_BYTES)
printf(_("min seg size (bytes) = %ju\n"), lim.shmmin);
else if (unit == UNIT_HUMAN)
printf(_("min seg size = %s\n"),
@@ -326,7 +338,7 @@ static void do_shm (char format, int unit)
else
printf ("%-10d %-10u", shmdsp->shm_perm.id, shmdsp->shm_perm.uid);
printf(" %-10o", shmdsp->shm_perm.mode & 0777);
- if (unit == UNIT_DEFAULT)
+ if (unit == UNIT_DEFAULT || unit == UNIT_BYTES)
printf(" %-10lu", shmdsp->shm_segsz);
else if (unit == UNIT_HUMAN)
printf(" %-10s",
@@ -460,14 +472,14 @@ static void do_msg (char format, int unit)
return;
printf (_("------ Messages Limits --------\n"));
printf (_("max queues system wide = %d\n"), lim.msgmni);
- if (unit == UNIT_DEFAULT)
+ if (unit == UNIT_DEFAULT || unit == UNIT_BYTES)
printf(_("max size of message (bytes) = %zu\n"),
lim.msgmax);
else if (unit == UNIT_HUMAN)
printf(_("max size of message = %s\n"),
size_to_human_string(SIZE_SUFFIX_1LETTER,
lim.msgmax));
- if (unit == UNIT_DEFAULT)
+ if (unit == UNIT_DEFAULT || unit == UNIT_BYTES)
printf(_("default max size of queue (bytes) = %d\n"),
lim.msgmnb);
else if (unit == UNIT_HUMAN)
@@ -485,13 +497,13 @@ static void do_msg (char format, int unit)
}
printf (_("------ Messages Status --------\n"));
printf (_("allocated queues = %d\n"), msginfo.msgpool);
- if (unit == UNIT_DEFAULT)
+ if (unit == UNIT_DEFAULT || unit == UNIT_BYTES)
printf(_("used headers = %d\n"), msginfo.msgmap);
else if (unit == UNIT_HUMAN)
printf(_("used headers = %s\n"),
size_to_human_string(SIZE_SUFFIX_1LETTER,
msginfo.msgmap));
- if (unit == UNIT_DEFAULT)
+ if (unit == UNIT_DEFAULT || unit == UNIT_BYTES)
printf(_("used space = %d bytes\n"), msginfo.msgtql);
else if (unit == UNIT_HUMAN)
printf(_("used space = %s\n"),
@@ -568,7 +580,7 @@ static void do_msg (char format, int unit)
else
printf ("%-10d %-10u", msgdsp->msg_perm.id, msgdsp->msg_perm.uid);
printf(" %-10o", msgdsp->msg_perm.mode & 0777);
- if (unit == UNIT_DEFAULT)
+ if (unit == UNIT_DEFAULT || unit == UNIT_BYTES)
printf(" %-12ld", msgdsp->q_cbytes);
else if (unit == UNIT_HUMAN)
printf(" %-12s",
@@ -598,7 +610,7 @@ static void print_shm(int shmid, int unit)
shmdata->shm_perm.cuid, shmdata->shm_perm.cgid);
printf(_("mode=%#o\taccess_perms=%#o\n"), shmdata->shm_perm.mode,
shmdata->shm_perm.mode & 0777);
- if (unit == UNIT_DEFAULT)
+ if (unit == UNIT_DEFAULT || unit == UNIT_BYTES)
printf(_("bytes=%ju\t"), shmdata->shm_segsz);
else if (unit == UNIT_HUMAN)
printf(_("size=%s\t"),
@@ -632,7 +644,7 @@ void print_msg(int msgid, int unit)
msgdata->msg_perm.uid, msgdata->msg_perm.uid,
msgdata->msg_perm.cuid, msgdata->msg_perm.cgid,
msgdata->msg_perm.mode);
- if (unit == UNIT_DEFAULT)
+ if (unit == UNIT_DEFAULT || unit == UNIT_BYTES)
printf(_("cbytes=%jd\tqbytes=%jd"), msgdata->q_cbytes,
msgdata->q_qbytes);
else if (unit == UNIT_HUMAN)
--
1.8.0.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH 1/3] ipcs: add --human readable size conversion option
2012-12-09 21:08 [PATCH 1/3] ipcs: add --human readable size conversion option Sami Kerola
2012-12-09 21:08 ` [PATCH 2/3] docs: swapon.8 option name fix Sami Kerola
2012-12-09 21:08 ` [PATCH 3/3] ipcs: add --bytes size output option Sami Kerola
@ 2012-12-10 9:06 ` Karel Zak
2012-12-10 23:38 ` Sami Kerola
2 siblings, 1 reply; 5+ messages in thread
From: Karel Zak @ 2012-12-10 9:06 UTC (permalink / raw)
To: Sami Kerola; +Cc: util-linux
On Sun, Dec 09, 2012 at 09:08:34PM +0000, Sami Kerola wrote:
> +.B \-\-human
> +Show print sizes in human readable format (e.g., 1K 234M 2G).
Nice idea, but what about create any smart function to print the
stuff to avoid all the "if (unit == UNIT_DEFAULT) else" in the code?
> + if (unit == UNIT_DEFAULT)
> + printf(_("max seg size (kbytes) = %ju\n"),
> + lim.shmmax / 1024);
> + else if (unit == UNIT_HUMAN)
> + printf(_("max seg size = %s\n"),
> + size_to_human_string(SIZE_SUFFIX_1LETTER,
> + lim.shmmax));
ipc_print(IPC_UNIT_KB, _("max seg size), lim.shmmax, "\n");
(append " (bytes) = " if there is not "=" at the end of the string)
> @@ -560,8 +632,17 @@ void print_msg(int msgid)
> msgdata->msg_perm.uid, msgdata->msg_perm.uid,
> msgdata->msg_perm.cuid, msgdata->msg_perm.cgid,
> msgdata->msg_perm.mode);
> - printf(_("cbytes=%jd\tqbytes=%jd\tqnum=%jd\tlspid=%d\tlrpid=%d\n"),
> - msgdata->q_cbytes, msgdata->q_qbytes, msgdata->q_qnum,
> + if (unit == UNIT_DEFAULT)
> + printf(_("cbytes=%jd\tqbytes=%jd"), msgdata->q_cbytes,
> + msgdata->q_qbytes);
ipc_print(IPC_UNIT_B, "cbytes=", msgdata->q_cbytes, "\t");
ipc_print(IPC_UNIT_B, "qbytes=", msgdata->q_qbytes, NULL);
... or so.
Karel
--
Karel Zak <kzak@redhat.com>
http://karelzak.blogspot.com
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 1/3] ipcs: add --human readable size conversion option
2012-12-10 9:06 ` [PATCH 1/3] ipcs: add --human readable size conversion option Karel Zak
@ 2012-12-10 23:38 ` Sami Kerola
0 siblings, 0 replies; 5+ messages in thread
From: Sami Kerola @ 2012-12-10 23:38 UTC (permalink / raw)
To: Karel Zak; +Cc: util-linux
On Mon, Dec 10, 2012 at 9:06 AM, Karel Zak <kzak@redhat.com> wrote:
Thank you for review Karel,
> On Sun, Dec 09, 2012 at 09:08:34PM +0000, Sami Kerola wrote:
>> +.B \-\-human
>> +Show print sizes in human readable format (e.g., 1K 234M 2G).
>
> Nice idea, but what about create any smart function to print the stuff
> to avoid all the "if (unit == UNIT_DEFAULT) else" in the code?
Yes, that could be done.
>> + if (unit == UNIT_DEFAULT)
>> + printf(_("max seg size (kbytes) = %ju\n"),
>> + lim.shmmax / 1024);
>> + else if (unit == UNIT_HUMAN)
>> + printf(_("max seg size = %s\n"),
>> + size_to_human_string(SIZE_SUFFIX_1LETTER,
>> + lim.shmmax));
>
> ipc_print(IPC_UNIT_KB, _("max seg size), lim.shmmax, "\n");
>
> (append " (bytes) = " if there is not "=" at the end of the string)
I hear the preference,
- Small changes are good change.
- But changes that result to understandable clean code are better.
The earlier patches are modified match with thinking above, and pushed to
my weekly branch;
The following changes since commit
72c9217951b7dd317a5d903d88291a9e87297969:
tests: unset *_DEBUG variables (2012-12-10 13:43:11 +0100)
are available in the git repository at:
git://github.com/kerolasa/lelux-utiliteetit.git 2012wk50
for you to fetch changes up to af1fe782e029b40fd99d5dccba8bb77ca2d2b0fa:
ipcs: add --bytes size output option (2012-12-10 22:58:08 +0000)
----------------------------------------------------------------
Sami Kerola (4):
docs: swapon.8 option name fix
ipcs: assist debugging
ipcs: add --human readable size conversion option
ipcs: add --bytes size output option
sys-utils/ipcs.c | 111
++++++++++++++++++++++++++++++++-------------------
sys-utils/ipcutils.c | 40 +++++++++++++++++++
sys-utils/ipcutils.h | 9 +++++
sys-utils/swapon.8 | 2 +-
4 files changed, 119 insertions(+), 43 deletions(-)
Here full diff of that patch that is completely rethought.
>From ee5ba39f135c195e2939c3234512782b6c97ddec Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Mon, 10 Dec 2012 20:39:26 +0000
Subject: ipcs: add --human readable size conversion option
Organization: Lastminute.com
Introduces new function ipc_print_size() which will call
size_to_human_string(), and handles the occasional '([k]bytes)' printing
if default size format is requested.
Reviewed-by: Karel Zak <kzak@redhat.com>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
sys-utils/ipcs.c | 88 ++++++++++++++++++++++++++++++++--------------------
sys-utils/ipcutils.c | 40 ++++++++++++++++++++++++
sys-utils/ipcutils.h | 9 ++++++
3 files changed, 103 insertions(+), 34 deletions(-)
diff --git a/sys-utils/ipcs.c b/sys-utils/ipcs.c
index 889c868..66c6302 100644
--- a/sys-utils/ipcs.c
+++ b/sys-utils/ipcs.c
@@ -33,14 +33,16 @@ enum output_formats {
TIME,
PID
};
+enum {
+ OPT_HUMAN = CHAR_MAX + 1
+};
-static void do_shm (char format);
-static void print_shm (int id);
+static void do_shm (char format, int unit);
+static void print_shm (int id, int unit);
static void do_sem (char format);
static void print_sem (int id);
-static void do_msg (char format);
-
-void print_msg (int id);
+static void do_msg (char format, int unit);
+static void print_msg (int id, int unit);
static void __attribute__ ((__noreturn__)) usage(FILE * out)
{
@@ -64,6 +66,7 @@ static void __attribute__ ((__noreturn__)) usage(FILE * out)
fputs(_(" -c, --creator show creator and owner\n"), out);
fputs(_(" -l, --limits show resource limits\n"), out);
fputs(_(" -u, --summary show status summary\n"), out);
+ fputs(_(" --human show sizes in human readable format\n"), out);
fprintf(out, USAGE_MAN_TAIL("ipcs(1)"));
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
}
@@ -72,6 +75,7 @@ int main (int argc, char **argv)
{
int opt, msg = 0, sem = 0, shm = 0, id=0, print=0;
char format = NOTSPECIFIED;
+ int unit = IPC_UNIT_DEFAULT;
static const struct option longopts[] = {
{"id", required_argument, NULL, 'i'},
{"shmems", no_argument, NULL, 'm'},
@@ -83,6 +87,7 @@ int main (int argc, char **argv)
{"creator", no_argument, NULL, 'c'},
{"limits", no_argument, NULL, 'l'},
{"summary", no_argument, NULL, 'u'},
+ {"human", no_argument, NULL, OPT_HUMAN},
{"version", no_argument, NULL, 'V'},
{"help", no_argument, NULL, 'h'},
{NULL, 0, NULL, 0}
@@ -127,6 +132,9 @@ int main (int argc, char **argv)
case 'u':
format = STATUS;
break;
+ case OPT_HUMAN:
+ unit = IPC_UNIT_HUMAN;
+ break;
case 'h':
usage(stdout);
case 'V':
@@ -139,11 +147,11 @@ int main (int argc, char **argv)
if (print) {
if (shm)
- print_shm (id);
+ print_shm (id, unit);
if (sem)
print_sem (id);
if (msg)
- print_msg (id);
+ print_msg (id, unit);
if (!shm && !sem && !msg )
usage (stderr);
} else {
@@ -152,7 +160,7 @@ int main (int argc, char **argv)
printf ("\n");
if (shm) {
- do_shm (format);
+ do_shm (format, unit);
printf ("\n");
}
if (sem) {
@@ -160,14 +168,14 @@ int main (int argc, char **argv)
printf ("\n");
}
if (msg) {
- do_msg (format);
+ do_msg (format, unit);
printf ("\n");
}
}
return EXIT_SUCCESS;
}
-static void do_shm (char format)
+static void do_shm (char format, int unit)
{
struct passwd *pw;
struct shm_data *shmds, *shmdsp;
@@ -181,10 +189,13 @@ static void do_shm (char format)
if (ipc_shm_get_limits(&lim))
return;
printf (_("max number of segments = %ju\n"), lim.shmmni);
- printf (_("max seg size (kbytes) = %ju\n"), lim.shmmax / 1024);
- printf (_("max total shared memory (kbytes) = %ju\n"),
- (lim.shmall / 1024) * getpagesize());
- printf (_("min seg size (bytes) = %ju\n"), lim.shmmin);
+ ipc_print_size(unit == IPC_UNIT_DEFAULT ? IPC_UNIT_KB : unit,
+ _("max seg size"), lim.shmmax, "\n", 0);
+ ipc_print_size(unit == IPC_UNIT_DEFAULT ? IPC_UNIT_KB : unit,
+ _("max total shared memory"),
+ lim.shmall * getpagesize(), "\n", 0);
+ ipc_print_size(unit == IPC_UNIT_DEFAULT ? IPC_UNIT_BYTES : unit,
+ _("min seg size"), lim.shmmin, "\n", 0);
return;
}
case STATUS:
@@ -248,7 +259,8 @@ static void do_shm (char format)
default:
printf (_("------ Shared Memory Segments --------\n"));
printf ("%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n",
- _("key"),_("shmid"),_("owner"),_("perms"),_("bytes"),
+ _("key"),_("shmid"),_("owner"),_("perms"),
+ unit == IPC_UNIT_HUMAN ? _("size") : _("bytes"),
_("nattch"),_("status"));
break;
}
@@ -295,9 +307,9 @@ static void do_shm (char format)
printf ("%-10d %-10.10s", shmdsp->shm_perm.id, pw->pw_name);
else
printf ("%-10d %-10u", shmdsp->shm_perm.id, shmdsp->shm_perm.uid);
- printf (" %-10o %-10lu %-10ld %-6s %-6s\n",
- shmdsp->shm_perm.mode & 0777,
- shmdsp->shm_segsz,
+ printf (" %-10o ", shmdsp->shm_perm.mode & 0777);
+ ipc_print_size(unit, NULL, shmdsp->shm_segsz, NULL, -10);
+ printf (" %-10ld %-6s %-6s\n",
shmdsp->shm_nattch,
shmdsp->shm_perm.mode & SHM_DEST ? _("dest") : " ",
shmdsp->shm_perm.mode & SHM_LOCKED ? _("locked") : " ");
@@ -410,7 +422,7 @@ static void do_sem (char format)
return;
}
-static void do_msg (char format)
+static void do_msg (char format, int unit)
{
struct passwd *pw;
struct msg_data *msgds, *msgdsp;
@@ -424,8 +436,10 @@ static void do_msg (char format)
return;
printf (_("------ Messages Limits --------\n"));
printf (_("max queues system wide = %d\n"), lim.msgmni);
- printf (_("max size of message (bytes) = %zu\n"), lim.msgmax);
- printf (_("default max size of queue (bytes) = %d\n"), lim.msgmnb);
+ ipc_print_size(unit == IPC_UNIT_DEFAULT ? IPC_UNIT_BYTES : unit,
+ _("max size of message"), lim.msgmax, "\n", 0);
+ ipc_print_size(unit == IPC_UNIT_DEFAULT ? IPC_UNIT_BYTES : unit,
+ _("default max size of queue"), lim.msgmnb, "\n", 0);
return;
}
case STATUS:
@@ -438,7 +452,8 @@ static void do_msg (char format)
printf (_("------ Messages Status --------\n"));
printf (_("allocated queues = %d\n"), msginfo.msgpool);
printf (_("used headers = %d\n"), msginfo.msgmap);
- printf (_("used space = %d bytes\n"), msginfo.msgtql);
+ ipc_print_size(unit, _("used space ="), msginfo.msgtql,
+ unit == IPC_UNIT_DEFAULT ? _(" bytes\n") : "\n", 0);
return;
}
case CREATOR:
@@ -463,7 +478,8 @@ static void do_msg (char format)
printf (_("------ Message Queues --------\n"));
printf ("%-10s %-10s %-10s %-10s %-12s %-12s\n",
_("key"), _("msqid"), _("owner"), _("perms"),
- _("used-bytes"), _("messages"));
+ unit == IPC_UNIT_HUMAN ? _("size") : _("used-bytes"),
+ _("messages"));
break;
}
@@ -508,10 +524,9 @@ static void do_msg (char format)
printf ("%-10d %-10.10s", msgdsp->msg_perm.id, pw->pw_name);
else
printf ("%-10d %-10u", msgdsp->msg_perm.id, msgdsp->msg_perm.uid);
- printf (" %-10o %-12ld %-12ld\n",
- msgdsp->msg_perm.mode & 0777,
- msgdsp->q_cbytes,
- msgdsp->q_qnum);
+ printf (" %-10o ", msgdsp->msg_perm.mode & 0777);
+ ipc_print_size(unit, NULL, msgdsp->q_cbytes, NULL, -12);
+ printf (" %-12ld\n", msgdsp->q_qnum);
break;
}
}
@@ -520,7 +535,7 @@ static void do_msg (char format)
return;
}
-static void print_shm(int shmid)
+static void print_shm(int shmid, int unit)
{
struct shm_data *shmdata;
@@ -535,8 +550,10 @@ static void print_shm(int shmid)
shmdata->shm_perm.cuid, shmdata->shm_perm.cgid);
printf(_("mode=%#o\taccess_perms=%#o\n"), shmdata->shm_perm.mode,
shmdata->shm_perm.mode & 0777);
- printf(_("bytes=%ju\tlpid=%u\tcpid=%u\tnattch=%jd\n"),
- shmdata->shm_segsz, shmdata->shm_lprid, shmdata->shm_cprid,
+ ipc_print_size(unit, unit == IPC_UNIT_HUMAN ? _("size=") : _("bytes="),
+ shmdata->shm_segsz, "\t", 0);
+ printf(_("lpid=%u\tcpid=%u\tnattch=%jd\n"),
+ shmdata->shm_lprid, shmdata->shm_cprid,
shmdata->shm_nattch);
printf(_("att_time=%-26.24s\n"),
shmdata->shm_atim ? ctime(&(shmdata->shm_atim)) : _("Not set"));
@@ -548,8 +565,7 @@ static void print_shm(int shmid)
ipc_shm_free_info(shmdata);
}
-
-void print_msg(int msgid)
+void print_msg(int msgid, int unit)
{
struct msg_data *msgdata;
@@ -563,8 +579,12 @@ void print_msg(int msgid)
msgdata->msg_perm.uid, msgdata->msg_perm.uid,
msgdata->msg_perm.cuid, msgdata->msg_perm.cgid,
msgdata->msg_perm.mode);
- printf(_("cbytes=%jd\tqbytes=%jd\tqnum=%jd\tlspid=%d\tlrpid=%d\n"),
- msgdata->q_cbytes, msgdata->q_qbytes, msgdata->q_qnum,
+ ipc_print_size(unit, unit == IPC_UNIT_HUMAN ? _("csize=") : _("cbytes="),
+ msgdata->q_cbytes, "\t", 0);
+ ipc_print_size(unit, unit == IPC_UNIT_HUMAN ? _("qsize=") : _("qbytes="),
+ msgdata->q_qbytes, "\t", 0);
+ printf("qnum=%jd\tlspid=%d\tlrpid=%d\n",
+ msgdata->q_qnum,
msgdata->q_lspid, msgdata->q_lrpid);
printf(_("send_time=%-26.24s\n"),
msgdata->q_stime ? ctime(&msgdata->q_stime) : _("Not set"));
diff --git a/sys-utils/ipcutils.c b/sys-utils/ipcutils.c
index 7d1f0d1..d81ca20 100644
--- a/sys-utils/ipcutils.c
+++ b/sys-utils/ipcutils.c
@@ -7,6 +7,7 @@
#include "path.h"
#include "pathnames.h"
#include "ipcutils.h"
+#include "strutils.h"
#ifndef SEMVMX
# define SEMVMX 32767 /* <= 32767 semaphore maximum value */
@@ -504,3 +505,42 @@ void ipc_print_perms(FILE *f, struct ipc_stat *is)
else
fprintf(f, " %-10u\n", is->gid);
}
+
+void ipc_print_size(int unit, char *msg, size_t size, const char *end,
+ int width)
+{
+ char format[16];
+
+ if (!msg)
+ /* NULL */ ;
+ else if (msg[strlen(msg) - 1] == '=')
+ printf("%s", msg);
+ else if (unit == IPC_UNIT_BYTES)
+ printf(_("%s (bytes) = "), msg);
+ else if (unit == IPC_UNIT_KB)
+ printf(_("%s (kbytes) = "), msg);
+ else
+ printf("%s = ", msg);
+
+ switch (unit) {
+ case IPC_UNIT_DEFAULT:
+ case IPC_UNIT_BYTES:
+ sprintf(format, "%%%dzu", width);
+ printf(format, size);
+ break;
+ case IPC_UNIT_KB:
+ sprintf(format, "%%%dzu", width);
+ printf(format, size / 1024);
+ break;
+ case IPC_UNIT_HUMAN:
+ sprintf(format, "%%%ds", width);
+ printf(format, size_to_human_string(SIZE_SUFFIX_1LETTER, size));
+ break;
+ default:
+ /* impossible occurred */
+ abort();
+ }
+
+ if (end)
+ printf("%s", end);
+}
diff --git a/sys-utils/ipcutils.h b/sys-utils/ipcutils.h
index 28b35c1..6383124 100644
--- a/sys-utils/ipcutils.h
+++ b/sys-utils/ipcutils.h
@@ -78,6 +78,14 @@ union semun {
# define KEY key
#endif
+/* Size printing in ipcs is using these. */
+enum {
+ IPC_UNIT_DEFAULT,
+ IPC_UNIT_BYTES,
+ IPC_UNIT_KB,
+ IPC_UNIT_HUMAN
+};
+
struct ipc_limits {
uint64_t shmmni; /* max number of segments */
uint64_t shmmax; /* max segment size */
@@ -110,6 +118,7 @@ struct ipc_stat {
};
extern void ipc_print_perms(FILE *f, struct ipc_stat *is);
+extern void ipc_print_size(int unit, char *msg, size_t size, const
char *end, int width);
/* See 'struct shmid_kernel' in kernel sources
*/
--
1.8.0.1
--
Sami Kerola
http://www.iki.fi/kerolasa/
^ permalink raw reply related [flat|nested] 5+ messages in thread
end of thread, other threads:[~2012-12-10 23:38 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-12-09 21:08 [PATCH 1/3] ipcs: add --human readable size conversion option Sami Kerola
2012-12-09 21:08 ` [PATCH 2/3] docs: swapon.8 option name fix Sami Kerola
2012-12-09 21:08 ` [PATCH 3/3] ipcs: add --bytes size output option Sami Kerola
2012-12-10 9:06 ` [PATCH 1/3] ipcs: add --human readable size conversion option Karel Zak
2012-12-10 23:38 ` Sami Kerola
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.