the_wildgoose
Reading mtime (stat) to microsecond accuracy
I tried using
- File.state(“some/file”)
and discovered that it truncates the timestamps to the nearest second. It seems to be a wrapper for the underlying erlang call:
- :file.read_file_info(‘some/file’, [{:time, :posix}])
..which returns times in whole seconds…
Any suggestions on how to most performantly access mtimes to maximum accuracy of the filesystem?
Equivalently, the use is debouncing reads of a small JSON file, so a possible workaround is a high speed way to checksum the file? Suggestions?
(The file is small, we have a problem (insert reasons) that means we need to acquire an expensive lock (100s of milliseconds) before reading it, so I need robust way to detect if it’s been changed since last read. Yes, long term we can work on improving the speed of locking, but…)
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated!
To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead.
Sta...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
Other Trending Topics
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #performance











First 9 of 9 Posts
jhogberg
Relying on time stamps, however accurate, is not a reliable way to check whether files have changed. Neither are content comparisons (checksums or otherwise) if there’s a possibility of the file being modified while you’re reading it. Try writing a NIF that uses
inotify(7)or similar instead and listen for changes.I have a feeling that you’re trying to solve the wrong problem though, why are you communicating over the file system?
D4no0
Since nowadays hashing is so common, there is a chance that the CPU you are using might have acceleration for some specific hashing algorithms. If I were to choose, I would go CRC at the beginning.
dimitarvp
I’m interested what’s your actual scenario that you eventually ended up with having to modify a JSON file and checking if you modified it recently.
the_wildgoose
I’m using inotify. I need to debounce multiple notifications from it.
Can I just add that I’m not hoping for an argument on the merits of mtimes and comfortable with their limitations.
Larger situation is that multiple processes across an embedded system are all editing a master JSON config file. Think of something like openwrt and it’s config system. You need 2 basic primitives, open with a shared lock and open with an exclusive lock. Due to the varied different implementations/programming languages that I need to support, I’m using flock to implement my locking. For various reasons the required incantations to support flock using exec under elixir are “slow”. This is due to calling the native “flock” binary, which itself needs to bring up a shell to be safe against process failure.
Longer term I will rewrite flock in rustler. I already wrote an implementation in zigler, but that seems to have lost traction and whilst the code runs fine on a number of platforms, it fails to execute correctly on arm32. Development time is the limitation here. I also see that there is a pull request for OTP to implement flock in OTP27, which should land in about 14 months or so. So I just need to get this project stood up until then
So, back to the original problem: I have a database file, its slow to lock it for (synchronised) reading. I can read it’s contents raw without a problem, eg to do a CRC check on it, or I can check it’s mtime to see if it changed since I last read it. The file is read regularly, but written very rarely. Goals are to avoid calling flock on every read. Cache is blown away on every inotify and when elixir alters the file. However, I’m still getting stale reads in some corner case. I’m looking to implement a safety net that also re-reads if the file has changed based on mtime (or perhaps something else)
OK, is the problem space clear enough? Any suggestions on accessing mtime that are neater than running exec “stat” and parsing the output?
the_wildgoose
Implement an API to access a JSON file…
People call the API to write stuff, then later something else reads it, etc, etc. We have a web interface, users can make changes and hit save. Then every time you open a page you read the config, de-json it, etc.
It’s an embedded processor. Reading files from slow flash, slow flocking, converting from JSON, etc, are all a performance hotspot. I think caching as a necessary evil around a slow resource is an accepted solution in general?
jhogberg
Fair enough, we’re just trying to help you better.
Since you just want to catch writes that somehow slip the
), I think the quickest way forward is to read the whole file and compare it with the cached version or a checksum thereof. If the files are reasonably small it shouldn’t be much more expensive than checking modification time and you won’t have to think about how to get a more accurate timestamp, so why not check if it works well enough?
inotifynet (how?dimitarvp
Feel free to ignore the following since you already outlined your time and energy budget constraints. I am still posting it because I feel it might be valuable now or in the future.
If you are looking for something so ubiquitous – even on ARM32 – then do consider SQLite, there are 1-2 good Elixir NIF libraries for it (and I never got around to writing my own due to various nasty factors). SQLite also has very solid JSON support, and its WAL / WAL2 features allow for safe parallel access, both read and write.
BradS2S
Here’s a NIF:
I compiled it using this command:
Here’s the module I wrapped it in:
(If you do a string it won’t work.)
{:ok, {seconds, nanoseconds}} = FileTime.get_file_time('file_as_char_list.txt'){:ok, datetime} = DateTime.from_unix(seconds)I tested it on a couple files and seemed to be working ok.
the_wildgoose
Thanks all. Some really great ideas here!
I think I can find a way to use the NIF idea as well. Thanks!