Tarantool development patches archive
 help / color / mirror / Atom feed
From: Maxim Kokryashkin via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: tarantool-patches@dev.tarantool.org, imun@tarantool.org,
	skaplun@tarantool.org, m.shishatskiy@tarantool.org
Subject: [Tarantool-patches] [PATCH luajit v2] memprof: add profile common section
Date: Wed,  6 Oct 2021 22:15:12 +0300
Message-ID: <20211006191512.3565471-1-m.kokryashkin@tarantool.org> (raw)
In-Reply-To: <YVwtjc6tBF3OPdCd@root>

A Lua API for sysprof needs to be introduced, but sysprof's C API is
quite similar with memprof's. Considering this, there are some
structures and functions that should be common among memprof's and
sysprof's implementations of Lua API to avoid duplication.

Part of tarantool/tarantool#781
---
GitHub branch: https://github.com/tarantool/luajit/tree/shishqa/gh-781-platform-and-lua-profiler

 src/lib_misc.c | 36 +++++++++++++++++++-----------------
 1 file changed, 19 insertions(+), 17 deletions(-)

diff --git a/src/lib_misc.c b/src/lib_misc.c
index 1dab08cc..22d29d78 100644
--- a/src/lib_misc.c
+++ b/src/lib_misc.c
@@ -8,8 +8,9 @@
 #define lib_misc_c
 #define LUA_LIB
 
-#include <stdio.h>
 #include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
 
 #include "lua.h"
 #include "lmisclib.h"
@@ -75,9 +76,7 @@ LJLIB_CF(misc_getmetrics)
 
 #include "lj_libdef.h"
 
-/* ----- misc.memprof module ---------------------------------------------- */
-
-#define LJLIB_MODULE_misc_memprof
+/* --------- profile common section --------------------------------------- */
 
 /*
 ** Yep, 8Mb. Tuned in order not to bother the platform with too often flushes.
@@ -85,9 +84,9 @@ LJLIB_CF(misc_getmetrics)
 #define STREAM_BUFFER_SIZE (8 * 1024 * 1024)
 
 /* Structure given as ctx to memprof writer and on_stop callback. */
-struct memprof_ctx {
-  /* Output file stream for data. */
-  FILE *stream;
+struct profile_ctx {
+  /* Output file descriptor for data. */
+  int fd;
   /* Profiled global_State for lj_mem_free at on_stop callback. */
   global_State *g;
   /* Buffer for data. */
@@ -96,13 +95,13 @@ struct memprof_ctx {
 
 /*
 ** Default buffer writer function.
-** Just call fwrite to the corresponding FILE.
+** Just call write to the corresponding descriptor.
 */
 static size_t buffer_writer_default(const void **buf_addr, size_t len,
 				    void *opt)
 {
-  struct memprof_ctx *ctx = opt;
-  FILE *stream = ctx->stream;
+  struct profile_ctx *ctx = opt;
+  const int fd = ctx->fd;
   const void * const buf_start = *buf_addr;
   const void *data = *buf_addr;
   size_t write_total = 0;
@@ -110,9 +109,9 @@ static size_t buffer_writer_default(const void **buf_addr, size_t len,
   lua_assert(len <= STREAM_BUFFER_SIZE);
 
   for (;;) {
-    const size_t written = fwrite(data, 1, len - write_total, stream);
+    const size_t written = write(fd, data, len - write_total);
 
-    if (LJ_UNLIKELY(written == 0)) {
+    if (LJ_UNLIKELY(written == -1)) {
       /* Re-tries write in case of EINTR. */
       if (errno != EINTR) {
 	/* Will be freed as whole chunk later. */
@@ -137,22 +136,25 @@ static size_t buffer_writer_default(const void **buf_addr, size_t len,
   return write_total;
 }
 
-/* Default on stop callback. Just close the corresponding stream. */
+/* Default on stop callback. Just close the corresponding descriptor. */
 static int on_stop_cb_default(void *opt, uint8_t *buf)
 {
-  struct memprof_ctx *ctx = opt;
-  FILE *stream = ctx->stream;
+  struct profile_ctx *ctx = opt;
+  const int fd = ctx->fd;
   UNUSED(buf);
   lj_mem_free(ctx->g, ctx, sizeof(*ctx));
-  return fclose(stream);
+  return close(fd);
 }
 
+/* ----- misc.memprof module ---------------------------------------------- */
+
+#define LJLIB_MODULE_misc_memprof
 /* local started, err, errno = misc.memprof.start(fname) */
 LJLIB_CF(misc_memprof_start)
 {
   struct lj_memprof_options opt = {0};
   const char *fname = strdata(lj_lib_checkstr(L, 1));
-  struct memprof_ctx *ctx;
+  struct profile_ctx *ctx;
   int memprof_status;
 
   /*
-- 
2.33.0


  reply	other threads:[~2021-10-06 19:15 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-08 17:50 [Tarantool-patches] [PATCH luajit 0/7] misc: introduce sysprof Maxim Kokryashkin via Tarantool-patches
2021-09-08 17:50 ` [Tarantool-patches] [PATCH luajit 1/7] core: save current frame top to lj_obj Maxim Kokryashkin via Tarantool-patches
2021-09-20 17:21   ` Sergey Kaplun via Tarantool-patches
2021-09-08 17:50 ` [Tarantool-patches] [PATCH luajit 2/7] core: separate the profiling timer from lj_profile Maxim Kokryashkin via Tarantool-patches
2021-09-21 11:13   ` Sergey Kaplun via Tarantool-patches
2021-09-23 11:37     ` Mikhail Shishatskiy via Tarantool-patches
2021-09-08 17:50 ` [Tarantool-patches] [PATCH luajit 3/7] memprof: move symtab to a separate module Maxim Kokryashkin via Tarantool-patches
2021-09-22  7:51   ` Sergey Kaplun via Tarantool-patches
2021-09-22  8:14     ` Sergey Kaplun via Tarantool-patches
2021-09-23 14:51   ` [Tarantool-patches] [PATCH luajit v2] " Maxim Kokryashkin via Tarantool-patches
2021-09-08 17:50 ` [Tarantool-patches] [PATCH luajit 4/7] core: introduce lua and platform profiler Maxim Kokryashkin via Tarantool-patches
2021-09-29  6:53   ` Sergey Kaplun via Tarantool-patches
2021-09-08 17:50 ` [Tarantool-patches] [PATCH luajit 5/7] memprof: add profile common section Maxim Kokryashkin via Tarantool-patches
2021-10-05 10:48   ` Sergey Kaplun via Tarantool-patches
2021-10-06 19:15     ` Maxim Kokryashkin via Tarantool-patches [this message]
2021-09-08 17:50 ` [Tarantool-patches] [PATCH luajit 6/7] sysprof: introduce Lua API Maxim Kokryashkin via Tarantool-patches
2021-10-05 15:36   ` Sergey Kaplun via Tarantool-patches
2021-09-08 17:50 ` [Tarantool-patches] [PATCH luajit 7/7] tools: introduce parsers for sysprof Maxim Kokryashkin via Tarantool-patches
2021-10-07 11:28   ` Sergey Kaplun via Tarantool-patches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211006191512.3565471-1-m.kokryashkin@tarantool.org \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=imun@tarantool.org \
    --cc=m.shishatskiy@tarantool.org \
    --cc=max.kokryashkin@gmail.com \
    --cc=skaplun@tarantool.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Tarantool development patches archive

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://lists.tarantool.org/tarantool-patches/0 tarantool-patches/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 tarantool-patches tarantool-patches/ https://lists.tarantool.org/tarantool-patches \
		tarantool-patches@dev.tarantool.org.
	public-inbox-index tarantool-patches

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git