/* tmdefs.h
****************************************************************************************************************
****************************************************************************************************************

    Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2020, 2021 Askar
    Almatov

    This file is part of Trafficmeter.

    Trafficmeter is free software: you can redistribute it and/or modify it under the terms of the GNU General
    Public License as published by the Free Software Foundation, either version 3 of the License, or (at your
    option) any later version.

    Trafficmeter is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
    implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
    License for more details.

    You should have received a copy of the GNU General Public License along with Trafficmeter. If not, see
    <http://www.gnu.org/licenses/>.
*/

#ifndef __TMDEFS_H
#define __TMDEFS_H 1

#include <ctime>

#include "config.h"

namespace trafficmeter
{
    constexpr const char*   CONFIG_FILE                 = "/usr/local/etc/tm.conf";
    constexpr const char*   PID_FILE                    = "/var/run/tmd.pid";
    constexpr const char*   LOG_FILE                    = "/var/log/tmd.log";
    constexpr const char*   FLOW_DIR                    = "flow";
    constexpr const char*   STAT_DIR                    = "stat";
    constexpr const char*   FLOW2_DIR                   = "flow2";
    constexpr const char*   PACKET_DIR                  = "packet";
    constexpr const char*   NAME_DB                     = "name.db";

    constexpr const int     PREFIX_HASH_LENGTH          = 10;           // bits
    constexpr const int     SUFFIX_HASH_LENGTH          = 10;           // bits
    constexpr const int     EXTENSION_LENGTH            = 8;            // bits
    constexpr const int     GROUP_FACTOR                = 300;          // seconds, group modulo factor
    constexpr const int     PACKET_FACTOR               = 10;           // seconds, write packet modulo factor
    constexpr const int     SOURCE_TIMEOUT              = 1;            // milliseconds
    constexpr const int     CHUNK_SIZE                  = 64;           // mebibytes
    constexpr const int     SUBSTRATE_SIZE              = 90176;        // bytes
    constexpr const int     BUCKET_PACKETS              = 16380;        // packets
    constexpr const int     MAX_FILES                   = 32768;        // number of files try to set rlimits

    constexpr const int     FLOW_INTERVALS              = 288;          // number of group intervals per file
    constexpr const int     STAT_INTERVALS              = 2016;         // number of group intervals per file
    constexpr const int     FLOW2_INTERVALS             = 4320;         // number of group intervals per file

    constexpr const int     DEFAULT_VERBOSITY           = 0;            // verbosity level of logging
    constexpr const int     DEFAULT_EXTRACTORS          = 2;            // number of threads
    constexpr const int     DEFAULT_FLOW_WRITERS        = 2;            // number of threads
    constexpr const int     DEFAULT_STAT_WRITERS        = 1;            // number of threads
    constexpr const int     DEFAULT_EXTRACTOR_CHUNKS    = 3;            // initial number of extractor chunks
    constexpr const int     DEFAULT_COLLECTION_CHUNKS   = 1;            // initial number of collection chunks

    constexpr const int     DEFAULT_FLOW_QUOTA          = 100;          // gibibytes (GiB)
    constexpr const int     DEFAULT_PACKET_QUOTA        = 100;          // gibibytes (GiB)
    constexpr const int     DEFAULT_STAT_QUOTA          = 40;           // gibibytes (GiB)
    constexpr const int     DEFAULT_FLOW2_QUOTA         = 1;            // gibibytes (GiB)

    constexpr const int     DEFAULT_WATCHING_DURATION   = 20;           // seconds
    constexpr const int     DEFAULT_DUMP_PACKETS        = 10;           // packets
    constexpr const int     DEFAULT_DUMP_BYTES          = 60;           // bytes

    //----------------------------------------------------------------------------------------------------------
    inline constexpr time_t
    moduloRoundUp( time_t time, int factor )
    {
        return time + factor - ( time % factor );
    }

    //----------------------------------------------------------------------------------------------------------
    inline constexpr time_t
    moduloRoundDown( time_t time, int factor )
    {
        return time - ( time % factor );
    }

    //----------------------------------------------------------------------------------------------------------
    inline constexpr unsigned
    align( unsigned size, unsigned alignment )
    {
        return ( size + alignment - 1 ) & ~( alignment - 1 );
    }

    //----------------------------------------------------------------------------------------------------------
    inline constexpr unsigned
    wordAlign( unsigned size )
    {
        return align( size, sizeof(void*) );
    }
}

#endif  // __TMDEFS_H
