Recent

Author Topic: Logging in Lazarus/FPC containerized microservice (Cloud Native Application)  (Read 2520 times)

Bogen85

  • Hero Member
  • *****
  • Posts: 703
Free Pascal's write ends up here: https://gitlab.com/freepascal.org/fpc/source/-/blob/main/rtl/inc/text.inc#L706
That looks like it is looping through the string.


So either fpwrite from baseunix or the write from libc should work.


Bogen85

  • Hero Member
  • *****
  • Posts: 703
https://gitlab.com/freepascal.org/fpc/source/-/blob/main/rtl/linux/ossysc.inc#L83

Code: Pascal  [Select][+][-]
  1. function Fpwrite(fd: cint;buf:pchar; nbytes : size_t): ssize_t; [public, alias : 'FPC_SYSC_WRITE'];
  2.  
  3. begin
  4.  Fpwrite:=do_syscall(syscall_nr_write,Fd,TSysParam(buf),nbytes);
  5. end;

So that should work as long as you are not using POSIX signals in your container app.

ionsem

  • Newbie
  • Posts: 5
https://gitlab.com/freepascal.org/fpc/source/-/blob/main/rtl/linux/ossysc.inc#L83

Code: Pascal  [Select][+][-]
  1. function Fpwrite(fd: cint;buf:pchar; nbytes : size_t): ssize_t; [public, alias : 'FPC_SYSC_WRITE'];
  2.  
  3. begin
  4.  Fpwrite:=do_syscall(syscall_nr_write,Fd,TSysParam(buf),nbytes);
  5. end;

So that should work as long as you are not using POSIX signals in your container app.
Quite impressive research done in the night ;-)
Thanks for sharing !

Thaddy

  • Hero Member
  • *****
  • Posts: 19470
  • Glad to be alive.
Well, less impressive research is simply reading the manual: https://www.freepascal.org/docs-html/3.2.2/rtl/baseunix/fpwrite.html
Any "programmer" that knows only one programming language is not a programmer

Bogen85

  • Hero Member
  • *****
  • Posts: 703
Well, less impressive research is simply reading the manual: https://www.freepascal.org/docs-html/3.2.2/rtl/baseunix/fpwrite.html

The manual does not say it is atomic.

I didn't know it's thread safe to call writeln from multiple threads without synchronization and didn't find this in documentation. So I did't even try to do it :).

I wanted to research it to make sure the underlying mechanism on Linux (which is what iosem's application is running on) is atomic.

fpwrite is not atomic by definition.
The Linux write syscall is atomic by definition, as it conforms to the POSIX standard.



Thaddy

  • Hero Member
  • *****
  • Posts: 19470
  • Glad to be alive.
fpwrite is not atomic by definition.
The Linux write syscall is atomic by definition, as it conforms to the POSIX standard.
That is bogus, Bogen85. On unixes is it the same call, only prefixed with fp. All unix syscalls have been prefixed with fp since a couple of years. (previously they where ad verbatim). I believe Marcov did that for some reason. You can see that from its implementation as described above. If Posix write is atomic, fpwrite is atomic, because it is the same thing.
« Last Edit: November 18, 2022, 12:54:48 pm by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

Bogen85

  • Hero Member
  • *****
  • Posts: 703
fpwrite is not atomic by definition.
The Linux write syscall is atomic by definition, as it conforms to the POSIX standard.
That is bogus, Bogen85. On unixes is it the same call, only prefixed with fp. All unix syscalls have been prefixed with fp since a couple of years. (previously they where ad verbatim). I believe Marcov did that for some reason.

Well, any unix that conforms to the POSIX standard the write syscall is supposed to be atomic.

Not sure what you are getting at as far as that being bogus.

@ionsem wanted documentation for what he needed to do to say it was atomic, which is at least how I understood him.

The question is about better approach with writing logs from all threads to stdout with Writeln without big performance impact...

And that it was desirable to for it to be performant...

So, performant and atomic.
« Last Edit: November 18, 2022, 01:25:56 pm by Bogen85 »

Thaddy

  • Hero Member
  • *****
  • Posts: 19470
  • Glad to be alive.
As I wrote, fpwrite is the same thing as Posix write. What you were stating, see my quote,  was simply very, very wrong.
Any "programmer" that knows only one programming language is not a programmer

Bogen85

  • Hero Member
  • *****
  • Posts: 703
As I wrote, fpwrite is the same thing as Posix write. What you were stating, see my quote,  was simply very, very wrong.

Oh, yeah. Agreed! https://www.freepascal.org/docs-html/3.2.2/rtl/baseunix/fpwrite.html does not say "atomic" in it's documentation...

But as you stated, it is the same as Posix write.

So I stand corrected. :)

Bogen85

  • Hero Member
  • *****
  • Posts: 703
I always assumed that the Linux (and Posix in general) write syscall was atomic.

This post prompted me to research it.

I know @ionsem asked for comments on the other mechanisms...

I just figured I'd find the most straightforward approach that does not involve additional complexities, as I want to do something something similar and want the same thing (Atomic and Performant).


 

TinyPortal © 2005-2018