diff --git a/README.md b/README.md index c5676446..2cfc21eb 100755 --- a/README.md +++ b/README.md @@ -257,9 +257,9 @@ This method always returns cache statistics, useful for determining how much wor Warnings such as _Application transferred too many scanlines_ are a good indicator you've set this value too low. ```javascript -var stats = sharp.cache(); // { current: 98, high: 115, limit: 100 } -sharp.cache(200); // { current: 98, high: 115, limit: 200 } -sharp.cache(50); // { current: 49, high: 115, limit: 50 } +var stats = sharp.cache(); // { current: 98, high: 115, limit: 100, queue: 0 } +sharp.cache(200); // { current: 98, high: 115, limit: 200, queue: 0 } +sharp.cache(50); // { current: 49, high: 115, limit: 50, queue: 0 } ``` ## Testing diff --git a/src/sharp.cc b/src/sharp.cc index 06ebf1c9..eebb5303 100755 --- a/src/sharp.cc +++ b/src/sharp.cc @@ -54,6 +54,9 @@ unsigned char const MARKER_JPEG[] = {0xff, 0xd8}; unsigned char const MARKER_PNG[] = {0x89, 0x50}; unsigned char const MARKER_WEBP[] = {0x52, 0x49}; +// How many tasks are in the queue? +volatile int queue_length = 0; + static bool ends_with(std::string const &str, std::string const &end) { return str.length() >= end.length() && 0 == str.compare(str.length() - end.length(), end.length(), end); } @@ -427,6 +430,8 @@ class ResizeWorker : public NanAsyncWorker { } delete baton; callback->Call(2, argv); + // Decrement queue length + g_atomic_int_dec_and_test(&queue_length); } private: @@ -480,6 +485,10 @@ NAN_METHOD(resize) { // Join queue for worker thread NanCallback *callback = new NanCallback(args[2].As()); NanAsyncQueueWorker(new ResizeWorker(callback, baton)); + + // Increment queue length + g_atomic_int_inc(&queue_length); + NanReturnUndefined(); } @@ -496,6 +505,7 @@ NAN_METHOD(cache) { cache->Set(NanNew("current"), NanNew(vips_tracked_get_mem() / 1048576)); cache->Set(NanNew("high"), NanNew(vips_tracked_get_mem_highwater() / 1048576)); cache->Set(NanNew("limit"), NanNew(vips_cache_get_max_mem() / 1048576)); + cache->Set(NanNew("queue"), NanNew(queue_length)); NanReturnValue(cache); } diff --git a/tests/parallel.js b/tests/parallel.js index c7c395cc..e72de08e 100755 --- a/tests/parallel.js +++ b/tests/parallel.js @@ -8,6 +8,10 @@ var inputJpg = path.join(__dirname, "fixtures/2569067123_aca715a2ee_o.jpg"); // var width = 720; var height = 480; +var timer = setInterval(function() { + console.dir(sharp.cache()); +}, 100); + async.mapSeries([1, 1, 2, 4, 8, 16, 32, 64, 128], function(parallelism, next) { var start = new Date().getTime(); async.times(parallelism, @@ -28,5 +32,6 @@ async.mapSeries([1, 1, 2, 4, 8, 16, 32, 64, 128], function(parallelism, next) { } ); }, function() { + clearInterval(timer); console.dir(sharp.cache()); });