+ } else {
+ NORM_ERR("invalid argument count for top");
+ free(td->s);
+ free(obj->data.opaque);
+ return 0;
+ }
+ return 1;
+}
+
+static char *format_time(unsigned long timeval, const int width)
+{
+ char buf[10];
+ unsigned long nt; // narrow time, for speed on 32-bit
+ unsigned cc; // centiseconds
+ unsigned nn; // multi-purpose whatever
+
+ nt = timeval;
+ cc = nt % 100; // centiseconds past second
+ nt /= 100; // total seconds
+ nn = nt % 60; // seconds past the minute
+ nt /= 60; // total minutes
+ if (width >= snprintf(buf, sizeof buf, "%lu:%02u.%02u",
+ nt, nn, cc)) {
+ return strndup(buf, text_buffer_size);
+ }
+ if (width >= snprintf(buf, sizeof buf, "%lu:%02u", nt, nn)) {
+ return strndup(buf, text_buffer_size);
+ }
+ nn = nt % 60; // minutes past the hour
+ nt /= 60; // total hours
+ if (width >= snprintf(buf, sizeof buf, "%lu,%02u", nt, nn)) {
+ return strndup(buf, text_buffer_size);
+ }
+ nn = nt; // now also hours
+ if (width >= snprintf(buf, sizeof buf, "%uh", nn)) {
+ return strndup(buf, text_buffer_size);
+ }
+ nn /= 24; // now days
+ if (width >= snprintf(buf, sizeof buf, "%ud", nn)) {
+ return strndup(buf, text_buffer_size);
+ }
+ nn /= 7; // now weeks
+ if (width >= snprintf(buf, sizeof buf, "%uw", nn)) {
+ return strndup(buf, text_buffer_size);
+ }
+ // well shoot, this outta' fit...
+ return strndup("<inf>", text_buffer_size);
+}
+
+static unsigned int top_name_width = 15;
+
+/* return zero on success, non-zero otherwise */
+int set_top_name_width(const char *s)
+{
+ if (!s)
+ return 0;
+ return !(sscanf(s, "%u", &top_name_width) == 1);
+}
+
+void print_top(struct text_object *obj, char *p, int p_max_size)
+{
+ struct information *cur = &info;
+ struct top_data *td = obj->data.opaque;
+ struct process **needed = 0;
+ int width;
+
+ if (!td)
+ return;
+
+ switch (obj->type) {
+ case OBJ_top:
+ needed = cur->cpu;
+ break;
+ case OBJ_top_mem:
+ needed = cur->memu;
+ break;
+ case OBJ_top_time:
+ needed = cur->time;
+ break;
+#ifdef IOSTATS
+ case OBJ_top_io:
+ needed = cur->io;
+ break;
+#endif /* IOSTATS */
+ default:
+ return;
+ }
+
+
+ if (needed[td->num]) {
+ char *timeval;
+
+ switch (td->type) {
+ case TOP_NAME:
+ if (needed[td->num]->name) {
+ width = MIN(p_max_size, (int)top_name_width + 1);
+ snprintf(p, width + 1, "%-*s", width,
+ needed[td->num]->name);
+ }
+ break;
+ case TOP_CPU:
+ width = MIN(p_max_size, 7);
+ snprintf(p, width, "%6.2f",
+ needed[td->num]->amount);
+ break;
+ case TOP_PID:
+ width = MIN(p_max_size, 6);
+ snprintf(p, width, "%5i",
+ needed[td->num]->pid);
+ break;
+ case TOP_MEM:
+ /* Calculate a percentage of residential mem from total mem available.
+ * Since rss is bytes and memmax kilobytes, dividing by 10 suffices here. */
+ width = MIN(p_max_size, 7);
+ snprintf(p, width, "%6.2f",
+ (float) ((float)needed[td->num]->rss / cur->memmax) / 10);
+ break;
+ case TOP_TIME:
+ width = MIN(p_max_size, 10);
+ timeval = format_time(
+ needed[td->num]->total_cpu_time, 9);
+ snprintf(p, width, "%9s", timeval);
+ free(timeval);
+ break;
+ case TOP_MEM_RES:
+ human_readable(needed[td->num]->rss,
+ p, p_max_size);
+ break;
+ case TOP_MEM_VSIZE:
+ human_readable(needed[td->num]->vsize,
+ p, p_max_size);
+ break;
+#ifdef IOSTATS
+ case TOP_READ_BYTES:
+ human_readable(needed[td->num]->read_bytes / update_interval,
+ p, p_max_size);
+ break;
+ case TOP_WRITE_BYTES:
+ human_readable(needed[td->num]->write_bytes / update_interval,
+ p, p_max_size);
+ break;
+ case TOP_IO_PERC:
+ width = MIN(p_max_size, 7);
+ snprintf(p, width, "%6.2f",
+ needed[td->num]->io_perc);
+ break;
+#endif