Skip to content

Commit

Permalink
migrate to c++11
Browse files Browse the repository at this point in the history
  • Loading branch information
deadtrickster committed Jan 2, 2018
1 parent 3b41065 commit ac5cac6
Show file tree
Hide file tree
Showing 13 changed files with 612 additions and 565 deletions.
9 changes: 9 additions & 0 deletions .dir-locals.el
@@ -0,0 +1,9 @@
((nil . ((indent-tabs-mode . nil)
(fill-column . 80)
(company-clang-arguments . ("-std=gnu++11"))))
(erlang-mode . ((flycheck-erlang-include-path . ("../include"
"../../include"
"../../../include"))
(flycheck-erlang-library-path . ("../../../_build/default/lib/prometheus"
"../../../_build/default/lib/prometheus/ebin"))
(erlang-indent-level . 2))))
48 changes: 22 additions & 26 deletions c_src/Makefile
Expand Up @@ -12,72 +12,68 @@ ERL_INTERFACE_LIB_DIR ?= $(shell erl -noshell -s init stop -eval "io:format(\"~s

C_SRC_DIR = $(CURDIR)
C_SRC_OUTPUT ?= $(CURDIR)/../priv/$(PROJECT).so
MEMTEST_OUTPUT ?= $(CURDIR)/../_build/prometheus_process_collector_backend
MEMTEST_OUTPUT ?= $(CURDIR)/../_build/memory_test

.PHONY: memory-test

# 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
CXX ?= c++
CXXFLAGS ?= -O3 -arch x86_64 -finline-functions
LDFLAGS ?= -arch x86_64 -flat_namespace -undefined suppress
PSOURCES = prometheus_process_info_macos.cc
else ifeq ($(UNAME_SYS), FreeBSD)
CC ?= cc
CEFLAGS ?= -D__STANDALONE_TEST__ -std=c99 -Wall -Wmissing-prototypes
CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes
CXXFLAGS ?= -O3 -finline-functions -Wall
CXX ?= c++
CXXEFLAGS ?= -D__STANDALONE_TEST__ -std=c++11 -Wall
CXXFLAGS ?= -O3 -finline-functions
PSOURCES = prometheus_process_info_freebsd.cc
else ifeq ($(UNAME_SYS), Linux)
CC ?= gcc
CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes
CEFLAGS ?= -D__STANDALONE_TEST__ -std=c99 -Wall -Wmissing-prototypes
CXXFLAGS ?= -O3 -finline-functions -Wall
CXX ?= g++
CXXEFLAGS ?= -D__STANDALONE_TEST__ -std=c++11 -Wall
CXXFLAGS ?= -O3 -finline-functions
PSOURCES = prometheus_process_info_linux.cc
endif

CFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR)
CXXFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR)
SOURCES = prometheus_process_collector_nif.cc $(PSOURCES)

CXXFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR) -std=c++11 -Wall
LDLIBS += -L $(ERL_INTERFACE_LIB_DIR) -lerl_interface -lei

ifeq ($(UNAME_SYS), OpenBSD)
LDLIBS += -lestdc++
else
LDLIBS += -lstdc++
endif

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) $<

memory-test:
@mkdir -p $(BASEDIR)/_build/
$(CC) $(CEFLAGS) $(LDLIBS) prometheus_process_collector_backend.c -o $(MEMTEST_OUTPUT)
$(CXX) $(CXXEFLAGS) $(LDLIBS) prometheus_process_info_mt.cc $(PSOURCES) -o $(MEMTEST_OUTPUT)
valgrind --leak-check=full --error-exitcode=1 $(MEMTEST_OUTPUT) 2

clean:
Expand Down
12 changes: 12 additions & 0 deletions c_src/prometheus_exceptions.h
@@ -0,0 +1,12 @@
#pragma once

#include <stdexcept>

namespace Prometheus
{
class ProcessInfoException : public std::runtime_error
{
public:
ProcessInfoException() : std::runtime_error("ProcessInfoException") {}
};
}
20 changes: 9 additions & 11 deletions c_src/prometheus_process_collector.h
@@ -1,5 +1,4 @@
#ifndef PROMETHEUS_PROCESS_COLLECTOR_BACKEND_H
#define PROMETHEUS_PROCESS_COLLECTOR_BACKEND_H
#pragma once

#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -49,18 +48,17 @@ struct prometheus_process_info

#ifdef __linux__

struct kinfo_proc {
struct timeval ki_start; /* starting time */
int ki_numthreads; /* XXXKSE number of threads in total */
unsigned long ki_size; /* virtual size */
unsigned long ki_rssize; /* resident size in pages */
struct rusage ki_rusage; /* process rusage statistics */
struct kinfo_proc
{
struct timeval ki_start; /* starting time */
int ki_numthreads; /* XXXKSE number of threads in total */
unsigned long ki_size; /* virtual size */
unsigned long ki_rssize; /* resident size in pages */
struct rusage ki_rusage; /* process rusage statistics */
};

#endif

#define PROCESS_INFO_COUNT 18

int fill_prometheus_process_info(pid_t pid, struct prometheus_process_info* prometheus_process_info);

#endif
int fill_prometheus_process_info(pid_t pid, struct prometheus_process_info *prometheus_process_info);

0 comments on commit ac5cac6

Please sign in to comment.