diff --git a/src/main.c b/src/main.c index 73781095..1ce808a5 100644 --- a/src/main.c +++ b/src/main.c @@ -93,8 +93,12 @@ int main(int argc, char *argv[]) { ret = -1; goto done; } - logr(info, "%zi bytes of input JSON loaded from %s, parsing.\n", bytes, args_is_set(opts, "inputFile") ? "file" : "stdin"); - cJSON *scene = cJSON_Parse(input); + char size_buf[64]; + logr(info, "%s of input JSON loaded from %s, parsing.\n", human_file_size(bytes, size_buf), args_is_set(opts, "inputFile") ? "file" : "stdin"); + struct timeval json_timer; + timer_start(&json_timer); + cJSON *scene = cJSON_ParseWithLength(input, bytes); + size_t json_ms = timer_get_ms(json_timer); if (!scene) { const char *errptr = cJSON_GetErrorPtr(); if (errptr) { @@ -103,6 +107,7 @@ int main(int argc, char *argv[]) { goto done; } } + logr(info, "JSON parse took %lums\n", json_ms); //FIXME: mmap() input free(input); diff --git a/src/utils/fileio.c b/src/utils/fileio.c index f73c0e91..a7ca7b93 100644 --- a/src/utils/fileio.c +++ b/src/utils/fileio.c @@ -122,7 +122,7 @@ void write_file(const unsigned char *buf, size_t bufsize, const char *filePath) //We determine the file size after saving, because the lodePNG library doesn't have a way to tell the compressed file size //This will work for all image formats unsigned long bytes = get_file_size(backupPath ? backupPath : filePath); - char *sizeString = human_file_size(bytes); + char *sizeString = human_file_size(bytes, NULL); logr(info, "Wrote %s to file.\n", sizeString); free(sizeString); } @@ -241,8 +241,7 @@ char *read_stdin(size_t *bytes) { return buf; } -// FIXME: Have this take a buffer instead of allocating it in here -char *human_file_size(unsigned long bytes) { +char *human_file_size(unsigned long bytes, char *stat_buf) { double kilobytes, megabytes, gigabytes, terabytes, petabytes, exabytes, zettabytes, yottabytes; // <- Futureproofing?! kilobytes = bytes / 1000.0; megabytes = kilobytes / 1000.0; @@ -258,7 +257,7 @@ char *human_file_size(unsigned long bytes) { // I *did* get it to go to yottabytes using __uint128_t, but that's // not in C99. Maybe in the future. - char *buf = calloc(64, sizeof(*buf)); + char *buf = stat_buf ? stat_buf : calloc(64, sizeof(*buf)); if (zettabytes >= 1000) { sprintf(buf, "%.02fYB", yottabytes); diff --git a/src/utils/fileio.h b/src/utils/fileio.h index 400f7a09..fa2722bf 100644 --- a/src/utils/fileio.h +++ b/src/utils/fileio.h @@ -27,7 +27,7 @@ enum fileType { }; enum fileType guess_file_type(const char *filePath); -char *human_file_size(unsigned long bytes); +char *human_file_size(unsigned long bytes, char *stat_buf); char *load_file(const char *filePath, size_t *bytes, struct file_cache *cache); // This is a more robust file writing function, that will seek alternate directories // if the specified one wasn't writeable. diff --git a/src/utils/loaders/textureloader.c b/src/utils/loaders/textureloader.c index 462c752c..6b9f9210 100644 --- a/src/utils/loaders/textureloader.c +++ b/src/utils/loaders/textureloader.c @@ -41,7 +41,7 @@ static struct texture *load_env_map(unsigned char *buf, size_t buflen, const cha logr(warning, "Error while decoding HDR from %s - Corrupted?\n", path); return NULL; } - char *fsbuf = human_file_size(buflen); + char *fsbuf = human_file_size(buflen, NULL); printf(" %s\n", fsbuf); free(fsbuf); return tex; diff --git a/src/utils/mempool.c b/src/utils/mempool.c index 21c84219..e4690ffe 100644 --- a/src/utils/mempool.c +++ b/src/utils/mempool.c @@ -35,8 +35,8 @@ void *allocBlock(struct block **head, size_t size) { if ((*head)->size + size > (*head)->capacity) { // Need to add a new block size_t nextSize = (*head)->capacity > size ? (*head)->capacity : size; - char *new_size = human_file_size(nextSize); - char *prev_size = human_file_size((*head)->size); + char *new_size = human_file_size(nextSize, NULL); + char *prev_size = human_file_size((*head)->size, NULL); logr(debug, "Appending a new block of size %s. Previous head occupancy: %s\n", new_size, prev_size); free(new_size); free(prev_size); @@ -58,7 +58,7 @@ void destroyBlocks(struct block *head) { numDestroyed++; head = prev; } - char *size = human_file_size(bytesfreed); + char *size = human_file_size(bytesfreed, NULL); logr(debug, "Destroyed %lu blocks, %s\n", numDestroyed, size); free(size); } diff --git a/src/utils/protocol/server.c b/src/utils/protocol/server.c index 6e1b709d..c79992f3 100644 --- a/src/utils/protocol/server.c +++ b/src/utils/protocol/server.c @@ -403,7 +403,7 @@ struct renderClient *syncWithClients(const struct renderer *r, size_t *count) { char *assetCache = cache_encode(r->state.file_cache); size_t transfer_bytes = strlen(assetCache) + strlen(r->sceneCache); - char *transfer_size = human_file_size(transfer_bytes); + char *transfer_size = human_file_size(transfer_bytes, NULL); logr(info, "Sending %s to %lu client%s...\n", transfer_size, clientCount, PLURAL(clientCount)); free(transfer_size); diff --git a/tests/test_fileio.h b/tests/test_fileio.h index 1610b1dd..3ccdd188 100644 --- a/tests/test_fileio.h +++ b/tests/test_fileio.h @@ -12,23 +12,23 @@ bool fileio_humanFileSize(void) { char *humanSize = NULL; - humanSize = human_file_size(600); + humanSize = human_file_size(600, NULL); test_assert(stringEquals(humanSize, "600B")); free(humanSize); - humanSize = human_file_size(1000); + humanSize = human_file_size(1000, NULL); test_assert(stringEquals(humanSize, "1.00kB")); free(humanSize); - humanSize = human_file_size(1000 * 1000); + humanSize = human_file_size(1000 * 1000, NULL); test_assert(stringEquals(humanSize, "1.00MB")); free(humanSize); - humanSize = human_file_size(1000 * 1000 * 1000); + humanSize = human_file_size(1000 * 1000 * 1000, NULL); test_assert(stringEquals(humanSize, "1.00GB")); free(humanSize); - humanSize = human_file_size((unsigned long)1000 * 1000 * 1000 * 1000); + humanSize = human_file_size((unsigned long)1000 * 1000 * 1000 * 1000, NULL); test_assert(stringEquals(humanSize, "1.00TB")); free(humanSize);