solanav

solanav

Hello everyone!

I’m currently working on a web spider/metadata extractor and because writing parsers for many file types would be too much work, I’m using libextractor.

I wrote a NIF which I learned only allows for functions that return in less than ~1ms. So I just changed this:

static ErlNifFunc funcs[] = {
  { "extract", 2, extract }
};

into this:

static ErlNifFunc funcs[] = {
  { "extract", 2, extract, ERL_NIF_DIRTY_JOB_CPU_BOUND }
};

The problem: it seems like the scheduler gets to 100% utilization which is bad because the spider downloads a lot of files in parallel so it becomes impossible to do things like launch the observer if I’m calling the NIF. Any idea if this is normal? Is there a way I can prevent the NIF from blocking the rest of the system?

Here is a picture of the observer while I test the calls to my main function:

Thanks!

Showing Posts 1 to 10

mpope

mpope

Is it possible to run erlang:system_info to check schedulers_state, scheduling_statistics, dirty_cpu_schedulers_online, dirty_cpu_schedulers

And the most useful would be to check schedulers using microstate accounting? msacc:start(1000), msacc:print(), msacc:stop(). Might not be wise to use it in a production env…

ityonemo

ityonemo

Can you set the number of dirty nif schedulers to number of cores - 1? Probably easiest way to test this is to do the following at startup:

:erlang.system_flag(:dirty_cpu_schedulers, System.schedulers_online() - 1)
NobbZ

NobbZ

Are you sure that your NIF is CPU bound and not IO bound?

solanav

solanav OP

I don’t know how useful this is, I’m just running iex -S mix on my NIF, it’s not running in production or anything, but I hope it helps.

Is it possible to run erlang:system_info to check schedulers_state, scheduling_statistics, dirty_cpu_schedulers_online, dirty_cpu_schedulers

iex(1)> {:ok, file_data} = File.read("/home/solanav/krptkn/final_form.png")
{:ok,
 <<137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 6, 50, 0,
   0, 2, 229, 8, 6, 0, 0, 0, 110, 46, 127, 76, 0, 0, 11, 218, 116, 69, 88, 116,
   109, 120, 102, 105, 108, 101, 0, ...>>}

iex(2)> Extractor.extract(file_data)
Extracting plugins...[OK] [0.046875]
Reading file binary...[OK] [0.000000]
Extracting the data...[OK] [0.218750]
Removing the plugins...[OK] [0.000000]
Creating list of data...[OK] [0.000000]
Destroying the data array...[OK] [0.000000]
[
  {'png', 'mimetype', 1, 'text/plain', 'image/png'},
  {'png', 'image dimensions', 1, 'text/plain', '1586x741'},
  {'mime', 'mimetype', 1, 'text/plain', 'image/png'},
  {'png', 'keywords', 1, 'text/plain', 'url encoded data'},
  {'thumbnailgtk', 'image dimensions', 1, 'text/plain', '1586x741'},
  {'thumbnailgtk', 'thumbnail', 2, 'image/png',  [137, 80, 78, 71, 13, 10, 26, 10]},
  {'gstreamer', 'mimetype', 1, 'text/plain', 'image/png'},
  {'gstreamer', 'video dimensions', 1, 'text/plain', '1586x741'},
  {'gstreamer', 'video depth', 1, 'text/plain', '32'},
  {'gstreamer', 'pixel aspect ratio', 1, 'text/plain', '1/1'}
]

iex(3)> :erlang.system_info(:schedulers_state)
{12, 12, 12}

iex(4)> :erlang.system_info(:scheduling_statistics)
[
  {:process_max, 0, 0},
  {:process_high, 0, 0},
  {:process_normal, 0, 0},
  {:process_low, 0, 0},
  {:port, 0, 0}
]

iex(5)> :erlang.system_info(:dirty_cpu_schedulers_online)
12

iex(6)> :erlang.system_info(:dirty_cpu_schedulers)
12

And the most useful would be to check schedulers using microstate accounting? msacc:start(1000), msacc:print(), msacc:stop(). Might not be wise to use it in a production env…

Here I did :msacc.start(1000), ran the NIF, printed and then stopped.

iex(8)> :msacc.print()
Average thread real-time    : 1001601 us
Accumulated system run-time :    7208 us
Average scheduler run-time  :     593 us

        Thread      aux check_io emulator       gc    other     port    sleep

Stats per thread:
     async( 0)    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%  100.00%
       aux( 1)    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%   99.99%
dirty_cpu_( 1)    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%  100.00%
dirty_cpu_( 2)    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%  100.00%
dirty_cpu_( 3)    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%  100.00%
dirty_cpu_( 4)    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%  100.00%
dirty_cpu_( 5)    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%  100.00%
dirty_cpu_( 6)    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%  100.00%
dirty_cpu_( 7)    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%  100.00%
dirty_cpu_( 8)    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%  100.00%
dirty_cpu_( 9)    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%  100.00%
dirty_cpu_(10)    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%  100.00%
dirty_cpu_(11)    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%  100.00%
dirty_cpu_(12)    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%  100.00%
dirty_io_s( 1)    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%  100.00%
dirty_io_s( 2)    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%  100.00%
dirty_io_s( 3)    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%  100.00%
dirty_io_s( 4)    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%  100.00%
dirty_io_s( 5)    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%  100.00%
dirty_io_s( 6)    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%  100.00%
dirty_io_s( 7)    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%  100.00%
dirty_io_s( 8)    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%  100.00%
dirty_io_s( 9)    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%  100.00%
dirty_io_s(10)    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%  100.00%
      poll( 0)    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%  100.00%
 scheduler( 1)    0.00%    0.05%    0.00%    0.00%    0.00%    0.00%   99.94%
 scheduler( 2)    0.00%    0.00%    0.00%    0.00%    0.06%    0.00%   99.94%
 scheduler( 3)    0.00%    0.00%    0.00%    0.00%    0.06%    0.00%   99.94%
 scheduler( 4)    0.00%    0.00%    0.00%    0.00%    0.07%    0.00%   99.93%
 scheduler( 5)    0.00%    0.00%    0.00%    0.00%    0.06%    0.00%   99.94%
 scheduler( 6)    0.00%    0.00%    0.00%    0.00%    0.06%    0.00%   99.94%
 scheduler( 7)    0.00%    0.00%    0.00%    0.00%    0.01%    0.00%   99.99%
 scheduler( 8)    0.00%    0.00%    0.00%    0.00%    0.06%    0.00%   99.94%
 scheduler( 9)    0.00%    0.00%    0.00%    0.00%    0.06%    0.00%   99.94%
 scheduler(10)    0.00%    0.00%    0.00%    0.00%    0.06%    0.00%   99.94%
 scheduler(11)    0.00%    0.00%    0.00%    0.00%    0.06%    0.00%   99.94%
 scheduler(12)    0.00%    0.00%    0.00%    0.00%    0.06%    0.00%   99.94%

Stats per type:
         async    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%  100.00%
           aux    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%   99.99%
dirty_cpu_sche    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%  100.00%
dirty_io_sched    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%  100.00%
          poll    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%  100.00%
     scheduler    0.00%    0.00%    0.00%    0.00%    0.05%    0.00%   99.94%
:ok
solanav

solanav OP

So I tried doing that and it seems like the result is the same, probably because the number of dirty schedulers was 12 so I guess not much changed.

solanav

solanav OP

Well I am not. Not really. I know libextractor (the library I am wrapping with this NIF) does not wait for user input and does not connect to the internet. But I think it may be running some code stored on dynamic libraries.

But I have tried changing it to IO scheduler and even though the peak was of less duration, it still peaked at 80-100%. The graph was almost the same but no dotted line, just a normal one.

nonblockio

nonblockio

It seems that the performance surge only least for one or two seconds from the chart. :msacc.start(1000) is kinda too short to catch the vibration since it only analyze scheduler in 1000ms. Could you increase the time of :msacc.start/1 and try to fit the surge in that period?

nonblockio

nonblockio

And could you provide the OTP version?

solanav

solanav OP

I’m using version 23.1 (tried both on WSL and on a ubuntu 20.04 virtual machine with the same results).

solanav

solanav OP

I can’t get it to work, the extractor won’t start running until :msacc.start() has finished. Is this code alright?

def test do
  {:ok, file_data} = File.read("/home/solanav/krptkn/final_form.png")

  :msacc.start(2_000)
  Extractor.extract(file_data)
  :msacc.print()
  :msacc.stop
end

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews