static void test_hashmap_size(void) { Hashmap *m; char *val1, *val2, *val3, *val4; val1 = strdup("my val"); assert_se(val1); val2 = strdup("my val"); assert_se(val2); val3 = strdup("my val"); assert_se(val3); val4 = strdup("my val"); assert_se(val4); assert_se(hashmap_size(NULL) == 0); assert_se(hashmap_buckets(NULL) == 0); m = hashmap_new(&string_hash_ops); hashmap_put(m, "Key 1", val1); hashmap_put(m, "Key 2", val2); hashmap_put(m, "Key 3", val3); hashmap_put(m, "Key 4", val4); assert_se(m); assert_se(hashmap_size(m) == 4); assert_se(hashmap_buckets(m) >= 4); hashmap_free_free(m); }
END_TEST START_TEST(test_map_put_delete) { hashmap *map; int res = hashmap_init(0, &map); fail_unless(res == 0); char buf[100]; void *out; int j; for (int i=0; i<100;i++) { snprintf((char*)&buf, 100, "test%d", i); j = 0 & i; out = (void *)&j; fail_unless(hashmap_put(map, (char*)buf, out) == 1); } fail_unless(hashmap_size(map) == 100); for (int i=0; i<100;i++) { snprintf((char*)&buf, 100, "test%d", i); fail_unless(hashmap_delete(map, (char*)buf) == 0); fail_unless(hashmap_size(map) == (100-i-1)); } fail_unless(hashmap_size(map) == 0); res = hashmap_destroy(map); fail_unless(res == 0); }
END_TEST START_TEST(test_map_put_clear_get) { hashmap *map; int res = hashmap_init(0, &map); fail_unless(res == 0); char buf[100]; void *out; for (int i=0; i<100;i++) { snprintf((char*)&buf, 100, "test%d", i); out = 0 & i; fail_unless(hashmap_put(map, (char*)buf, out) == 1); } fail_unless(hashmap_size(map) == 100); fail_unless(hashmap_clear(map) == 0); fail_unless(hashmap_size(map) == 0); for (int i=0; i<100;i++) { snprintf((char*)&buf, 100, "test%d", i); fail_unless(hashmap_get(map, (char*)buf, &out) == -1); } res = hashmap_destroy(map); fail_unless(res == 0); }
DECLARE_TEST( hashmap, erase ) { hashmap_t* map = hashmap_allocate( 0, 0 ); void* prev = 0; EXPECT_EQ( hashmap_lookup( map, 0 ), 0 ); EXPECT_EQ( hashmap_size( map ), 0 ); prev = hashmap_insert( map, 0, map ); EXPECT_EQ( prev, 0 ); EXPECT_EQ( hashmap_size( map ), 1 ); EXPECT_TRUE( hashmap_has_key( map, 0 ) ); prev = hashmap_erase( map, 0 ); EXPECT_EQ( prev, map ); EXPECT_EQ( hashmap_size( map ), 0 ); EXPECT_FALSE( hashmap_has_key( map, 0 ) ); prev = hashmap_erase( map, 0 ); EXPECT_EQ( prev, 0 ); EXPECT_EQ( hashmap_size( map ), 0 ); EXPECT_FALSE( hashmap_has_key( map, 0 ) ); prev = hashmap_erase( map, (hash_t)(uintptr_t)map ); EXPECT_EQ( prev, 0 ); EXPECT_EQ( hashmap_size( map ), 0 ); EXPECT_FALSE( hashmap_has_key( map, (hash_t)(uintptr_t)map ) ); hashmap_deallocate( map ); return 0; }
void hashmap_create_with_allocator(hashmap *hm, int n_size, allocator alloc) { hm->alloc = alloc; hm->buckets_x = n_size; hm->buckets = hm->alloc.cmalloc(hashmap_size(hm) * sizeof(hashmap_bucket)); for(int i = 0; i < hashmap_size(hm); i++) { hm->buckets[i].first = NULL; } hm->reserved = 0; }
static void _hashmap_seek_next(hashmap *hm, iterator *iter) { if(iter->inow >= hashmap_size(hm)) { iter->vnow = NULL; iter->ended = 1; return; } do { iter->vnow = hm->buckets[iter->inow++].first; } while(iter->vnow == NULL && iter->inow < hashmap_size(hm)); }
_public_ int sd_lldp_get_neighbors(sd_lldp *lldp, sd_lldp_neighbor ***ret) { sd_lldp_neighbor **l = NULL, *n; Iterator i; int k = 0, r; assert_return(lldp, -EINVAL); assert_return(ret, -EINVAL); if (hashmap_isempty(lldp->neighbor_by_id)) { /* Special shortcut */ *ret = NULL; return 0; } l = new0(sd_lldp_neighbor*, hashmap_size(lldp->neighbor_by_id)); if (!l) return -ENOMEM; r = lldp_start_timer(lldp, NULL); if (r < 0) { free(l); return r; } HASHMAP_FOREACH(n, lldp->neighbor_by_id, i) l[k++] = sd_lldp_neighbor_ref(n); assert((size_t) k == hashmap_size(lldp->neighbor_by_id)); /* Return things in a stable order */ qsort(l, k, sizeof(sd_lldp_neighbor*), neighbor_compare_func); *ret = l; return k; }
static int lldp_make_space(sd_lldp *lldp, size_t extra) { usec_t t = USEC_INFINITY; bool changed = false; assert(lldp); /* Remove all entries that are past their TTL, and more until at least the specified number of extra entries * are free. */ for (;;) { _cleanup_(sd_lldp_neighbor_unrefp) sd_lldp_neighbor *n = NULL; n = prioq_peek(lldp->neighbor_by_expiry); if (!n) break; sd_lldp_neighbor_ref(n); if (hashmap_size(lldp->neighbor_by_id) > LESS_BY(lldp->neighbors_max, extra)) goto remove_one; if (t == USEC_INFINITY) t = now(clock_boottime_or_monotonic()); if (n->until > t) break; remove_one: lldp_neighbor_unlink(n); lldp_callback(lldp, SD_LLDP_EVENT_REMOVED, n); changed = true; } return changed; }
/* hashmap_grow - extend existing map */ static int hashmap_grow(hashmap_t *map) { hashmap_entry_t *ht; hashmap_entry_t *next; unsigned old_size = map->size; hashmap_entry_t **h = map->data; hashmap_entry_t **old_entries = h; if(hashmap_size(map, primes[++map->idx]) == -1) { return -1; } while (old_size-- > 0) { for (ht = *h++; ht; ht = next) { next = ht->next; hashmap_link(map, ht); } } if (old_entries) { free((char *) old_entries); old_entries = NULL; } return 0; }
static void bus_track_add_to_queue(sd_bus_track *track) { assert(track); /* Adds the bus track object to the queue of objects we should dispatch next, subject to a number of * conditions. */ /* Already in the queue? */ if (track->in_queue) return; /* if we are currently in the process of adding a new name, then let's not enqueue this just yet, let's wait * until the addition is complete. */ if (track->n_adding > 0) return; /* still referenced? */ if (hashmap_size(track->names) > 0) return; /* Nothing to call? */ if (!track->handler) return; /* Already closed? */ if (!track->in_list) return; LIST_PREPEND(queue, track->bus->track_queue, track); track->in_queue = true; }
/** * Create an iterator * @param map the map to make an iterator of * @return the iterator or NULL */ hashmap_iterator *hashmap_iterator_create( hashmap *map ) { hashmap_iterator *iter = calloc( 1, sizeof(hashmap_iterator) ); if ( iter != NULL ) { iter->position = 0; iter->num_keys = hashmap_size(map); iter->values = (char**)calloc( iter->num_keys, sizeof(char*) ); if ( iter->values != NULL ) { int i,k; for ( k=0,i=0;i<map->num_buckets;i++ ) { if ( map->buckets[i] != NULL ) { struct bucket *b = map->buckets[i]; while ( b != NULL ) { iter->values[k++] = b->key; b = b->next; } } } } else { warning("hashmap_iterator: couldn't allocate key array\n"); } } else warning("hashmap_iterator: failed to allocate object\n"); return iter; }
static int client_acquire(Context *context, uint64_t id, Client **_c) { char *watch = NULL; Client *c; int r; assert(context); assert(_c); c = hashmap_get(context->clients, &id); if (c) { *_c = c; return 0; } if (hashmap_size(context->clients) >= CLIENTS_MAX) return -ENOBUFS; r = hashmap_ensure_allocated(&context->clients, uint64_hash_func, uint64_compare_func); if (r < 0) return r; c = new0(Client, 1); if (!c) return -ENOMEM; c->id = id; r = hashmap_put(context->clients, &c->id, c); if (r < 0) goto fail; c->context = context; if (asprintf(&watch, "type='signal'," "sender='org.freedesktop.DBus'," "path='/org/freedesktop/DBus'," "interface='org.freedesktop.DBus'," "member='NameOwnerChanged'," "arg0=':1.%llu'", (unsigned long long) id) < 0) { r = -ENOMEM; goto fail; } r = sd_bus_add_match(context->bus, watch, on_name_owner_changed, c); if (r < 0) { free(watch); goto fail; } c->watch = watch; *_c = c; return 0; fail: client_free(c); return r; }
sysview_seat *sysview_seat_free(sysview_seat *seat) { if (!seat) return NULL; assert(!seat->public); assert(hashmap_size(seat->device_map) == 0); assert(hashmap_size(seat->session_map) == 0); if (seat->name) hashmap_remove_value(seat->context->seat_map, seat->name, seat); hashmap_free(seat->device_map); hashmap_free(seat->session_map); free(seat->name); free(seat); return NULL; }
END_TEST START_TEST(test_map_put) { bloom_hashmap *map; int res = hashmap_init(0, &map); fail_unless(res == 0); fail_unless(hashmap_size(map) == 0); char buf[100]; for (int i=0; i<100;i++) { snprintf((char*)&buf, 100, "test%d", i); fail_unless(hashmap_put(map, (char*)buf, NULL) == 1); } fail_unless(hashmap_size(map) == 100); res = hashmap_destroy(map); fail_unless(res == 0); }
DECLARE_TEST(hashmap, allocation) { hashmap_t* map = hashmap_allocate(0, 0); EXPECT_EQ(hashmap_size(map), 0); EXPECT_EQ(hashmap_lookup(map, 0), 0); EXPECT_EQ(hashmap_lookup(map, (hash_t)(uintptr_t)map), 0); hashmap_deallocate(map); map = hashmap_allocate(13, 127); EXPECT_EQ(hashmap_size(map), 0); EXPECT_EQ(hashmap_lookup(map, 0), 0); EXPECT_EQ(hashmap_lookup(map, (hash_t)(uintptr_t)map), 0); hashmap_deallocate(map); return 0; }
// wrapper for hashmap_iterator struct room_iterator *room_iterator_new(struct room *r) { if (!r) return NULL; if (hashmap_size(r->clients) == 0) return NULL; struct room_iterator *room_it = NULL; room_it = (struct room_iterator *) mem_malloc(sizeof(struct room_iterator)); room_it->it = hashmap_get_iterator(r->clients); return room_it; }
void TestHashmaplinked_New( CuTest * tc ) { hashmap_t *hm; hm = hashmap_new(__uint_hash, __uint_compare, 11); CuAssertTrue(tc, 0 == hashmap_count(hm)); CuAssertTrue(tc, 11 == hashmap_size(hm)); hashmap_freeall(hm); }
/** * 添加客户端 * @param connectfd [description] * @param addr [description] * @return [description] */ int client_add(int connectfd, struct sockaddr_in addr, client_t *client) { /*加锁*/ if (pthread_mutex_lock(&mutex) != 0) { perror("pthread_mutex_lock"); exit(EXIT_FAILURE); } if (hashmap_size(client_map) >= MAX_CLIENT) { bzero(buf, MAX_BUF + 1); sprintf(buf, "连接数太多了"); send(connectfd, buf, strlen(buf), 0); /*解锁锁*/ if (pthread_mutex_unlock(&mutex) != 0) { perror("pthread_mutex_unlock"); exit(EXIT_FAILURE); } return -1; //连接数太多 } int ret; client_elem *c_el = (client_elem *)malloc(sizeof(client_elem)); sprintf(c_el->key, "%d", connectfd); c_el->addr = addr; c_el->client = client; ret = hashmap_put(client_map, c_el->key, c_el); assert(ret == HMAP_S_OK); printf("hashmap_size: %d fd:%s\n", hashmap_size(client_map), c_el->key); /*解锁锁*/ if (pthread_mutex_unlock(&mutex) != 0) { perror("pthread_mutex_unlock"); exit(EXIT_FAILURE); } return hashmap_size(client_map); }
void TestHashmaplinked_PutEnsuresCapacity( CuTest * tc ) { hashmap_t *hm; hm = hashmap_new(__uint_hash, __uint_compare, 1); hashmap_put(hm, (void *) 50, (void *) 92); hashmap_put(hm, (void *) 51, (void *) 92); CuAssertTrue(tc, 2 == hashmap_count(hm)); CuAssertTrue(tc, 2 == hashmap_size(hm)); hashmap_freeall(hm); }
DECLARE_TEST( hashmap, insert ) { hashmap_t* map = hashmap_allocate( 0, 0 ); void* prev = 0; EXPECT_EQ( hashmap_lookup( map, 0 ), 0 ); prev = hashmap_insert( map, 0, map ); EXPECT_EQ( prev, 0 ); prev = hashmap_insert( map, 0, map ); EXPECT_EQ( prev, map ); prev = hashmap_insert( map, 0, 0 ); EXPECT_EQ( prev, map ); prev = hashmap_insert( map, 0, map ); EXPECT_EQ( prev, 0 ); prev = hashmap_insert( map, (hash_t)(uintptr_t)map, map ); EXPECT_EQ( prev, 0 ); EXPECT_EQ( hashmap_size( map ), 2 ); EXPECT_EQ( hashmap_lookup( map, 0 ), map ); EXPECT_EQ( hashmap_lookup( map, (hash_t)(uintptr_t)map ), map ); hashmap_insert( map, 0, 0 ); hashmap_insert( map, (hash_t)(uintptr_t)map, 0 ); EXPECT_EQ( hashmap_size( map ), 2 ); EXPECT_EQ( hashmap_lookup( map, 0 ), 0 ); EXPECT_EQ( hashmap_lookup( map, (hash_t)(uintptr_t)map ), 0 ); hashmap_deallocate( map ); return 0; }
void hashmap_clear(hashmap *hm) { hashmap_node *node = NULL; hashmap_node *tmp = NULL; for(unsigned int i = 0; i < hashmap_size(hm); i++) { node = hm->buckets[i].first; while(node != NULL) { tmp = node; node = node->next; hm->alloc.cfree(tmp->pair.key); hm->alloc.cfree(tmp->pair.val); hm->alloc.cfree(tmp); hm->reserved--; } hm->buckets[i].first = NULL; } }
END_TEST START_TEST(test_map_delete_no_keys) { hashmap *map; int res = hashmap_init(0, &map); fail_unless(res == 0); fail_unless(hashmap_size(map) == 0); char buf[100]; for (int i=0; i<100;i++) { snprintf((char*)&buf, 100, "test%d", i); fail_unless(hashmap_delete(map, (char*)buf) == -1); } res = hashmap_destroy(map); fail_unless(res == 0); }
static void test_hashmap_move(void) { Hashmap *m, *n; char *val1, *val2, *val3, *val4, *r; log_info("%s", __func__); val1 = strdup("val1"); assert_se(val1); val2 = strdup("val2"); assert_se(val2); val3 = strdup("val3"); assert_se(val3); val4 = strdup("val4"); assert_se(val4); m = hashmap_new(&string_hash_ops); n = hashmap_new(&string_hash_ops); hashmap_put(n, "key 1", strdup(val1)); hashmap_put(m, "key 1", val1); hashmap_put(m, "key 2", val2); hashmap_put(m, "key 3", val3); hashmap_put(m, "key 4", val4); assert_se(hashmap_move(n, NULL) == 0); assert_se(hashmap_move(n, m) == 0); assert_se(hashmap_size(m) == 1); r = hashmap_get(m, "key 1"); assert_se(r && streq(r, "val1")); r = hashmap_get(n, "key 1"); assert_se(r && streq(r, "val1")); r = hashmap_get(n, "key 2"); assert_se(r && streq(r, "val2")); r = hashmap_get(n, "key 3"); assert_se(r && streq(r, "val3")); r = hashmap_get(n, "key 4"); assert_se(r && streq(r, "val4")); hashmap_free_free(m); hashmap_free_free(n); }
/** * Parse function */ int parse_resource(const char* path, parser_data_t* resource_data) { xmlSAXHandler saxHandler; memset(&saxHandler, 0, sizeof(saxHandler)); if(xmlSAXVersion(&saxHandler, 2)!=0){ error(2,errno,"error initializing SAX2 parser"); } saxHandler.startDocument = &start_document; saxHandler.endDocument = &end_document; saxHandler.startElementNs = &startElementNs; saxHandler.endElementNs = &endElementNs; saxHandler.characters = &chars; saxHandler.cdataBlock = &sax_nop; saxHandler.comment = &sax_nop; saxHandler.error = &sax_error; saxHandler.fatalError = &sax_fatal; saxHandler.warning = &sax_warn; // Creating context parse_context_t context; memset(&context, 0, sizeof(context)); context.devices = resource_data->devices; context.capabilities = resource_data->capabilities; context.current_devicedef = NULL; int sax_error = xmlSAXUserParseFile(&saxHandler, &context, path); if(sax_error) { error(2, 0, "SAX error parsing file: %s", path); } error(0,0, "parsed %d devices", hashmap_size(context.devices)); xmlCleanupParser(); return 0; }
static void test_catalog_import_one(void) { _cleanup_hashmap_free_free_free_ Hashmap *h = NULL; char *payload; Iterator j; const char *input = "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \ "Subject: message\n" \ "\n" \ "payload\n"; const char *expect = "Subject: message\n" \ "\n" \ "payload\n"; h = test_import(input, -1, 0); assert_se(hashmap_size(h) == 1); HASHMAP_FOREACH(payload, h, j) { assert_se(streq(expect, payload)); }
END_TEST START_TEST(test_map_put_grow) { bloom_hashmap *map; int res = hashmap_init(32, &map); // Only 32 slots fail_unless(res == 0); char buf[100]; for (int i=0; i<1000;i++) { snprintf((char*)&buf, 100, "test%d", i); fail_unless(hashmap_put(map, (char*)buf, NULL) == 1); } int val = 0; fail_unless(hashmap_iter(map, iter_test, (void*)&val) == 0); fail_unless(val == 1000); fail_unless(hashmap_size(map) == 1000); res = hashmap_destroy(map); fail_unless(res == 0); }
static void test_catalog_importing(void) { Hashmap *h; struct strbuf *sb; assert_se(h = hashmap_new(&catalog_hash_ops)); assert_se(sb = strbuf_new()); #define BUF "xxx" test_import(h, sb, BUF, sizeof(BUF), -EINVAL); #undef BUF assert_se(hashmap_isempty(h)); log_debug("----------------------------------------"); #define BUF \ "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededede\n" \ "Subject: message\n" \ "\n" \ "payload\n" test_import(h, sb, BUF, sizeof(BUF), -EINVAL); #undef BUF log_debug("----------------------------------------"); #define BUF \ "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \ "Subject: message\n" \ "\n" \ "payload\n" test_import(h, sb, BUF, sizeof(BUF), 0); #undef BUF assert_se(hashmap_size(h) == 1); log_debug("----------------------------------------"); hashmap_free_free(h); strbuf_cleanup(sb); }
static void clear_test( void ) { int res; hashmap_t hashmap; size_t size; unsigned int existed; res = hashmap_init( &hashmap ); assert( 0 == res ); res = hashmap_insert( &hashmap, "test", 1 ); assert( 0 == res ); hashmap_clear( &hashmap ); size = hashmap_size( &hashmap ); assert( 0 == size ); res = hashmap_insert_existed( &hashmap, "test", 1, &existed ); assert( 0 == res ); assert( 0 == existed ); hashmap_term( &hashmap ); }
static void display(Hashmap *a) { Iterator i; Group *g; Group **array; signed path_columns; unsigned rows, n = 0, j, maxtcpu = 0, maxtpath = 3; /* 3 for ellipsize() to work properly */ char buffer[MAX3(21, FORMAT_BYTES_MAX, FORMAT_TIMESPAN_MAX)]; assert(a); if (!terminal_is_dumb()) fputs(ANSI_HOME_CLEAR, stdout); array = alloca(sizeof(Group*) * hashmap_size(a)); HASHMAP_FOREACH(g, a, i) if (g->n_tasks_valid || g->cpu_valid || g->memory_valid || g->io_valid) array[n++] = g; qsort_safe(array, n, sizeof(Group*), group_compare); /* Find the longest names in one run */ for (j = 0; j < n; j++) { unsigned cputlen, pathtlen; format_timespan(buffer, sizeof(buffer), (usec_t) (array[j]->cpu_usage / NSEC_PER_USEC), 0); cputlen = strlen(buffer); maxtcpu = MAX(maxtcpu, cputlen); pathtlen = strlen(array[j]->path); maxtpath = MAX(maxtpath, pathtlen); } if (arg_cpu_type == CPU_PERCENT) xsprintf(buffer, "%6s", "%CPU"); else xsprintf(buffer, "%*s", maxtcpu, "CPU Time"); rows = lines(); if (rows <= 10) rows = 10; if (on_tty()) { const char *on, *off; path_columns = columns() - 36 - strlen(buffer); if (path_columns < 10) path_columns = 10; on = ansi_highlight_underline(); off = ansi_underline(); printf("%s%s%-*s%s %s%7s%s %s%s%s %s%8s%s %s%8s%s %s%8s%s%s\n", ansi_underline(), arg_order == ORDER_PATH ? on : "", path_columns, "Control Group", arg_order == ORDER_PATH ? off : "", arg_order == ORDER_TASKS ? on : "", arg_count == COUNT_PIDS ? "Tasks" : arg_count == COUNT_USERSPACE_PROCESSES ? "Procs" : "Proc+", arg_order == ORDER_TASKS ? off : "", arg_order == ORDER_CPU ? on : "", buffer, arg_order == ORDER_CPU ? off : "", arg_order == ORDER_MEMORY ? on : "", "Memory", arg_order == ORDER_MEMORY ? off : "", arg_order == ORDER_IO ? on : "", "Input/s", arg_order == ORDER_IO ? off : "", arg_order == ORDER_IO ? on : "", "Output/s", arg_order == ORDER_IO ? off : "", ansi_normal()); } else path_columns = maxtpath; for (j = 0; j < n; j++) { _cleanup_free_ char *ellipsized = NULL; const char *path; if (on_tty() && j + 6 > rows) break; g = array[j]; path = isempty(g->path) ? "/" : g->path; ellipsized = ellipsize(path, path_columns, 33); printf("%-*s", path_columns, ellipsized ?: path); if (g->n_tasks_valid) printf(" %7" PRIu64, g->n_tasks); else fputs(" -", stdout); if (arg_cpu_type == CPU_PERCENT) { if (g->cpu_valid) printf(" %6.1f", g->cpu_fraction*100); else fputs(" -", stdout); } else printf(" %*s", maxtcpu, format_timespan(buffer, sizeof(buffer), (usec_t) (g->cpu_usage / NSEC_PER_USEC), 0)); printf(" %8s", maybe_format_bytes(buffer, sizeof(buffer), g->memory_valid, g->memory)); printf(" %8s", maybe_format_bytes(buffer, sizeof(buffer), g->io_valid, g->io_input_bps)); printf(" %8s", maybe_format_bytes(buffer, sizeof(buffer), g->io_valid, g->io_output_bps)); putchar('\n'); } }
static int display(Hashmap *a) { Iterator i; Group *g; Group **array; signed path_columns; unsigned rows, n = 0, j, maxtcpu = 0, maxtpath = 0; char buffer[MAX3(21, FORMAT_BYTES_MAX, FORMAT_TIMESPAN_MAX)]; assert(a); /* Set cursor to top left corner and clear screen */ if (on_tty()) fputs("\033[H" "\033[2J", stdout); array = alloca(sizeof(Group*) * hashmap_size(a)); HASHMAP_FOREACH(g, a, i) if (g->n_tasks_valid || g->cpu_valid || g->memory_valid || g->io_valid) array[n++] = g; qsort_safe(array, n, sizeof(Group*), group_compare); /* Find the longest names in one run */ for (j = 0; j < n; j++) { unsigned cputlen, pathtlen; format_timespan(buffer, sizeof(buffer), (nsec_t) (array[j]->cpu_usage / NSEC_PER_USEC), 0); cputlen = strlen(buffer); maxtcpu = MAX(maxtcpu, cputlen); pathtlen = strlen(array[j]->path); maxtpath = MAX(maxtpath, pathtlen); } if (arg_cpu_type == CPU_PERCENT) snprintf(buffer, sizeof(buffer), "%6s", "%CPU"); else snprintf(buffer, sizeof(buffer), "%*s", maxtcpu, "CPU Time"); rows = lines(); if (rows <= 10) rows = 10; if (on_tty()) { path_columns = columns() - 36 - strlen(buffer); if (path_columns < 10) path_columns = 10; printf("%s%-*s%s %s%7s%s %s%s%s %s%8s%s %s%8s%s %s%8s%s\n\n", arg_order == ORDER_PATH ? ON : "", path_columns, "Path", arg_order == ORDER_PATH ? OFF : "", arg_order == ORDER_TASKS ? ON : "", "Tasks", arg_order == ORDER_TASKS ? OFF : "", arg_order == ORDER_CPU ? ON : "", buffer, arg_order == ORDER_CPU ? OFF : "", arg_order == ORDER_MEMORY ? ON : "", "Memory", arg_order == ORDER_MEMORY ? OFF : "", arg_order == ORDER_IO ? ON : "", "Input/s", arg_order == ORDER_IO ? OFF : "", arg_order == ORDER_IO ? ON : "", "Output/s", arg_order == ORDER_IO ? OFF : ""); } else path_columns = maxtpath; for (j = 0; j < n; j++) { char *p; if (on_tty() && j + 5 > rows) break; g = array[j]; p = ellipsize(g->path, path_columns, 33); printf("%-*s", path_columns, p ? p : g->path); free(p); if (g->n_tasks_valid) printf(" %7u", g->n_tasks); else fputs(" -", stdout); if (arg_cpu_type == CPU_PERCENT) { if (g->cpu_valid) printf(" %6.1f", g->cpu_fraction*100); else fputs(" -", stdout); } else printf(" %*s", maxtcpu, format_timespan(buffer, sizeof(buffer), (nsec_t) (g->cpu_usage / NSEC_PER_USEC), 0)); if (g->memory_valid) printf(" %8s", format_bytes(buffer, sizeof(buffer), g->memory)); else fputs(" -", stdout); if (g->io_valid) { printf(" %8s", format_bytes(buffer, sizeof(buffer), g->io_input_bps)); printf(" %8s", format_bytes(buffer, sizeof(buffer), g->io_output_bps)); } else fputs(" - -", stdout); putchar('\n'); } return 0; }