Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
deadtrickster committed Jul 25, 2016
0 parents commit 9c2bcb0
Show file tree
Hide file tree
Showing 10 changed files with 419 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.rebar3
_*
.eunit
*.o
*.beam
*.plt
*.swp
*.swo
.erlang.cookie
ebin
log
erl_crash.dump
.rebar
logs
_build
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016, Ilya Khaprov <dead.trickster@gmail.com>.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
prometheus_process_collector
=====

An OTP library

Build
-----

$ rebar3 compile
74 changes: 74 additions & 0 deletions c_src/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Based on c_src.mk from erlang.mk by Loic Hoguin <essen@ninenines.eu>

CURDIR := $(shell pwd)
BASEDIR := $(abspath $(CURDIR)/..)

PROJECT ?= $(notdir $(BASEDIR))
PROJECT := $(strip $(PROJECT))

ERTS_INCLUDE_DIR ?= $(shell erl -noshell -s init stop -eval "io:format(\"~s/erts-~s/include/\", [code:root_dir(), erlang:system_info(version)]).")
ERL_INTERFACE_INCLUDE_DIR ?= $(shell erl -noshell -s init stop -eval "io:format(\"~s\", [code:lib_dir(erl_interface, include)]).")
ERL_INTERFACE_LIB_DIR ?= $(shell erl -noshell -s init stop -eval "io:format(\"~s\", [code:lib_dir(erl_interface, lib)]).")

C_SRC_DIR = $(CURDIR)
C_SRC_OUTPUT ?= $(CURDIR)/../priv/$(PROJECT).so

# System type and C compiler/flags.

UNAME_SYS := $(shell uname -s)
ifeq ($(UNAME_SYS), Darwin)
CC ?= cc
CFLAGS ?= -O3 -std=c99 -arch x86_64 -finline-functions -Wall -Wmissing-prototypes
CXXFLAGS ?= -O3 -arch x86_64 -finline-functions -Wall
LDFLAGS ?= -arch x86_64 -flat_namespace -undefined suppress
else ifeq ($(UNAME_SYS), FreeBSD)
CC ?= cc
CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes
CXXFLAGS ?= -O3 -finline-functions -Wall
else ifeq ($(UNAME_SYS), Linux)
CC ?= gcc
CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes
CXXFLAGS ?= -O3 -finline-functions -Wall
endif

CFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR)
CXXFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR)

LDLIBS += -L $(ERL_INTERFACE_LIB_DIR) -lerl_interface -lei
LDFLAGS += -shared

# Verbosity.

c_verbose_0 = @echo " C " $(?F);
c_verbose = $(c_verbose_$(V))

cpp_verbose_0 = @echo " CPP " $(?F);
cpp_verbose = $(cpp_verbose_$(V))

link_verbose_0 = @echo " LD " $(@F);
link_verbose = $(link_verbose_$(V))

SOURCES := $(shell find $(C_SRC_DIR) -type f \( -name "*.c" -o -name "*.C" -o -name "*.cc" -o -name "*.cpp" \))
OBJECTS = $(addsuffix .o, $(basename $(SOURCES)))

COMPILE_C = $(c_verbose) $(CC) $(CFLAGS) $(CPPFLAGS) -c
COMPILE_CPP = $(cpp_verbose) $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c

$(C_SRC_OUTPUT): $(OBJECTS)
@mkdir -p $(BASEDIR)/priv/
$(link_verbose) $(CC) $(OBJECTS) $(LDFLAGS) $(LDLIBS) -o $(C_SRC_OUTPUT)

%.o: %.c
$(COMPILE_C) $(OUTPUT_OPTION) $<

%.o: %.cc
$(COMPILE_CPP) $(OUTPUT_OPTION) $<

%.o: %.C
$(COMPILE_CPP) $(OUTPUT_OPTION) $<

%.o: %.cpp
$(COMPILE_CPP) $(OUTPUT_OPTION) $<

clean:
@rm -f $(C_SRC_OUTPUT) $(OBJECTS)
100 changes: 100 additions & 0 deletions c_src/prometheus_process_collector_nif.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#define _GNU_SOURCE
#include "erl_nif.h"
#include <dirent.h>
#include <unistd.h>

ERL_NIF_TERM mk_error(ErlNifEnv* env, const char* mesg);
ERL_NIF_TERM mk_atom(ErlNifEnv* env, const char* atom);

#define MAXBUFLEN 1024

ERL_NIF_TERM
mk_atom(ErlNifEnv* env, const char* atom)
{
ERL_NIF_TERM ret;

if(!enif_make_existing_atom(env, atom, &ret, ERL_NIF_LATIN1))
{
return enif_make_atom(env, atom);
}

return ret;
}

ERL_NIF_TERM
mk_error(ErlNifEnv* env, const char* mesg)
{
return enif_make_tuple2(env, mk_atom(env, "error"), mk_atom(env, mesg));
}

static ERL_NIF_TERM
sc_clk_tck(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
if(argc != 0)
{
return enif_make_badarg(env);
}

long result = sysconf(_SC_CLK_TCK);

if(result == -1 || result == 0)
{
return mk_error(env, "sysconf_fail");
}

return enif_make_ulong(env, result);
}

static ERL_NIF_TERM
sc_pagesize(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
if(argc != 0)
{
return enif_make_badarg(env);
}

long result = sysconf(_SC_PAGESIZE);

if(result == -1 || result == 0)
{
return mk_error(env, "sysconf_fail");
}

return enif_make_ulong(env, result);
}

static ERL_NIF_TERM
files_count(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
long file_count = 0;
DIR* dirp;
struct dirent* entry;
char path[MAXBUFLEN];

enif_get_string(env, argv[0], path, 1024, ERL_NIF_LATIN1);

if(argc != 1)
{
return enif_make_badarg(env);
}

dirp = opendir(path);
if (dirp == NULL) {
return mk_error(env, "opendir_fail");
}
while ((entry = readdir(dirp)) != NULL) {
if (entry->d_type == DT_REG) {
file_count++;
}
}
closedir(dirp);
return enif_make_ulong(env, file_count);
}

static ErlNifFunc nif_funcs[] = {
{"sc_clk_tck", 0, sc_clk_tck},
{"sc_pagesize", 0, sc_pagesize},
{"files_count", 1, files_count}
};

ERL_NIF_INIT(prometheus_process_collector, nif_funcs, NULL, NULL, NULL, NULL);
Binary file added priv/prometheus_process_collector.so
Binary file not shown.
9 changes: 9 additions & 0 deletions rebar.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{erl_opts, [debug_info]}.
{deps, [{prometheus, "1.6.0"}]}.

{pre_hooks,
[{"linux", compile, "make -C c_src"}]}.
{post_hooks,
[{"linux", clean, "make -C c_src clean"}]}.

{plugins, [rebar3_hex]}.
1 change: 1 addition & 0 deletions rebar.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{<<"prometheus">>,{pkg,<<"prometheus">>,<<"1.6.0">>},0}].
16 changes: 16 additions & 0 deletions src/prometheus_process_collector.app.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{application, prometheus_process_collector,
[{description, "Prometheus.io process collector"},
{vsn, "0.1.0"},
{registered, []},
{applications,
[kernel,
stdlib
]},
{env,[]},
{modules, []},

{maintainers, ["Ilya Khaprov"]},
{licenses, ["MIT"]},
{links, [{"Github", "https://github.com/deadtrickster/prometheus_process_collector.erl"},
{"Prometheus.io Client", "https://github.com/deadtrickster/prometheus.erl"}]}
]}.
Loading

0 comments on commit 9c2bcb0

Please sign in to comment.