Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gRPC auto threadiness #1271

Merged
merged 5 commits into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions falco.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ http_output:
# grpc:
# enabled: true
# bind_address: "0.0.0.0:5060"
# threadiness: 8
# threadiness: 0
leodido marked this conversation as resolved.
Show resolved Hide resolved
leodido marked this conversation as resolved.
Show resolved Hide resolved
# private_key: "/etc/falco/certs/server.key"
# cert_chain: "/etc/falco/certs/server.crt"
# root_certs: "/etc/falco/certs/ca.crt"
Expand All @@ -191,7 +191,8 @@ http_output:
grpc:
enabled: false
bind_address: "unix:///var/run/falco.sock"
threadiness: 8
# when threadiness is 0, Falco automatically guesses it depending on the number of online cores
threadiness: 0

# gRPC output service.
# By default it is off.
Expand Down
6 changes: 6 additions & 0 deletions userspace/engine/falco_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ std::string wrap_text(const std::string& str, uint32_t initial_pos, uint32_t ind
return ret;
}

uint32_t hardware_concurrency()
{
auto hc = std::thread::hardware_concurrency();
return hc ? hc : 1;
}

void readfile(const std::string& filename, std::string& data)
{
std::ifstream file(filename.c_str(), std::ios::in);
Expand Down
4 changes: 4 additions & 0 deletions userspace/engine/falco_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ limitations under the License.
#include <fstream>
#include <iostream>
#include <string>
#include <thread>
#include <nonstd/string_view.hpp>

#pragma once
Expand All @@ -34,6 +35,9 @@ namespace utils
std::string wrap_text(const std::string& str, uint32_t initial_pos, uint32_t indent, uint32_t line_len);

void readfile(const std::string& filename, std::string& data);

uint32_t hardware_concurrency();

namespace network
{
static const std::string UNIX_SCHEME("unix://");
Expand Down
10 changes: 6 additions & 4 deletions userspace/falco/configuration.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2019 The Falco Authors.
Copyright (C) 2020 The Falco Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -20,6 +20,7 @@ limitations under the License.
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "falco_utils.h"

#include "configuration.h"
#include "logger.h"
Expand Down Expand Up @@ -148,11 +149,12 @@ void falco_configuration::init(string conf_filename, list<string> &cmdline_optio

m_grpc_enabled = m_config->get_scalar<bool>("grpc", "enabled", false);
m_grpc_bind_address = m_config->get_scalar<string>("grpc", "bind_address", "0.0.0.0:5060");
m_grpc_threadiness = m_config->get_scalar<uint32_t>("grpc", "threadiness", 8); // todo > limit it to avoid overshubscription? std::thread::hardware_concurrency()
m_grpc_threadiness = m_config->get_scalar<uint32_t>("grpc", "threadiness", 0);
if(m_grpc_threadiness == 0)
{
throw logic_error("error reading config file (" + m_config_file + "): gRPC threadiness must be greater than 0");
m_grpc_threadiness = falco::utils::hardware_concurrency();
}
// todo > else limit threadiness to avoid oversubscription?
m_grpc_private_key = m_config->get_scalar<string>("grpc", "private_key", "/etc/falco/certs/server.key");
m_grpc_cert_chain = m_config->get_scalar<string>("grpc", "cert_chain", "/etc/falco/certs/server.crt");
m_grpc_root_certs = m_config->get_scalar<string>("grpc", "root_certs", "/etc/falco/certs/ca.crt");
Expand Down Expand Up @@ -344,4 +346,4 @@ void falco_configuration::set_cmdline_option(const string &opt)
{
m_config->set_scalar(keyval.first, keyval.second);
}
}
}
2 changes: 1 addition & 1 deletion userspace/falco/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class falco_configuration
bool m_time_format_iso_8601;

bool m_grpc_enabled;
int m_grpc_threadiness;
uint32_t m_grpc_threadiness;
std::string m_grpc_bind_address;
std::string m_grpc_private_key;
std::string m_grpc_cert_chain;
Expand Down
1 change: 1 addition & 0 deletions userspace/falco/falco.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,7 @@ int falco_init(int argc, char **argv)
// gRPC server
if(config.m_grpc_enabled)
{
falco_logger::log(LOG_INFO, "gRPC server threadiness equals to " + to_string(config.m_grpc_threadiness) + "\n");
// TODO(fntlnz,leodido): when we want to spawn multiple threads we need to have a queue per thread, or implement
// different queuing mechanisms, round robin, fanout? What we want to achieve?
grpc_server.init(
Expand Down