<commit_before>// -*- mode: c; tab-width: 4; indent-tabs-mode: 1; st-rulers: [70] -*-
// vim: ts=8 sw=8 ft=c noet
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2013 Pagoda Box, Inc.  All rights reserved.
 */
#include <dirent.h>		/* open directory, get listings */
#include <locale.h>		/* set program locale */
#include <stdio.h>		/* standard buffered input/output */
#include <stdlib.h>		/* standard library definitions */
#include <syslog.h>		/* definitions for system error logging */
#include <sys/time.h>	/* time types */
#include <sys/param.h>
#include <sys/stat.h>
#include <unistd.h>		/* standard symbolic constants and types */

#include "api.h"
#include "config.h"
#include "vtepd.h"
#include "log.h"
#include "util/util.h"
#include "route.h"

/* server global state */
vtep_server_t server; 

static char
*human_readable(double value,int in_bits)
{
	if(value == 0 && in_bits)
		return "0 bytes";
	if(value == 0)
		return "0 bits";

	char tmp[22];
	char *modifier = " KMGTPEZY";
	int index = 0;
	while(value > 1024 && index < 8){
		value = value / 1024;
		index++;

	}
	if(in_bits){
		if(index == 0 )
			sprintf(tmp, "%.0f bytes", value);
		else
			sprintf(tmp, "%.1f %cB", value, modifier[index]);
	}else{
		if(index == 0 )
			sprintf(tmp, "%.0f bits", value);
		else
			sprintf(tmp, "%.1f %cb", value, modifier[index]);
	}
	return strdup(tmp);
}

static void
stats_report(uv_timer_t* handle, int status)
{
	double every_time = server.dump_stats_every/1000;
	vtep_log(VTEPD_DEBUG,"UDP stats:");
	vtep_log(VTEPD_DEBUG,"  packets in: %d out: %d",server.udp_recv_count,server.udp_send_count);
	vtep_log(VTEPD_DEBUG,"  data in: %s out: %s",human_readable(server.udp_recv_amount,1),human_readable(server.udp_send_amount,1));
	vtep_log(VTEPD_DEBUG,"  throughput in: %s/s out: %s/s",human_readable(server.udp_recv_amount*8/every_time,0),human_readable(server.udp_send_amount*8/every_time,0));

	server.udp_send_count = 0;
	server.udp_send_amount = 0;
	server.udp_recv_count = 0;
	server.udp_recv_amount = 0;
}

static void
create_pidfile(void)
{
	/* Try to write the pid file in a best-effort way. */
	FILE *fp = fopen(server.pidfile,"w");
	if (fp) {
		fprintf(fp,"%d\n",(int)getpid());
		fclose(fp);
	}
}

static void
daemonize(void)
{
	int fd;

	if (fork() != 0) exit(0); /* parent exits */
	setsid(); /* create a new session */

	/* Every output goes to /dev/null. If VTEP is daemonized but
	* the 'logfile' is set to 'stdout' in the configuration file
	* it will not log at all. */
	if ((fd = open("/dev/null", O_RDWR, 0)) != -1) {
		dup2(fd, STDIN_FILENO);
		dup2(fd, STDOUT_FILENO);
		dup2(fd, STDERR_FILENO);<commit_msg>more fixes<commit_after>// -*- mode: c; tab-width: 4; indent-tabs-mode: 1; st-rulers: [70] -*-
// vim: ts=8 sw=8 ft=c noet
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2013 Pagoda Box, Inc.  All rights reserved.
 */
#include <dirent.h>		/* open directory, get listings */
#include <locale.h>		/* set program locale */
#include <stdio.h>		/* standard buffered input/output */
#include <stdlib.h>		/* standard library definitions */
#include <syslog.h>		/* definitions for system error logging */
#include <sys/time.h>	/* time types */
#include <sys/param.h>
#include <sys/stat.h>
#include <unistd.h>		/* standard symbolic constants and types */

#include "api.h"
#include "config.h"
#include "vtepd.h"
#include "log.h"
#include "util/util.h"
#include "route.h"

/* server global state */
vtep_server_t server; 

static char
*human_readable(double value,int in_bits)
{
	if(value == 0 && in_bits)
		return "0 bytes";
	if(value == 0)
		return "0 bits";

	char tmp[22];
	char *modifier = " KMGTPEZY";
	int index = 0;
	while(value > 1024 && index < 8){
		value = value / 1024;
		index++;

	}
	if(in_bits){
		if(index == 0 )
			sprintf(tmp, "%.0f bytes", value);
		else
			sprintf(tmp, "%.1f %cB", value, modifier[index]);
	}else{
		if(index == 0 )
			sprintf(tmp, "%.0f bits", value);
		else
			sprintf(tmp, "%.1f %cb", value, modifier[index]);
	}
	return strdup(tmp);
}

static void
create_pidfile(void)
{
	/* Try to write the pid file in a best-effort way. */
	FILE *fp = fopen(server.pidfile,"w");
	if (fp) {
		fprintf(fp,"%d\n",(int)getpid());
		fclose(fp);
	}
}

static void
daemonize(void)
{
	int fd;

	if (fork() != 0) exit(0); /* parent exits */
	setsid(); /* create a new session */

	/* Every output goes to /dev/null. If VTEP is daemonized but
	* the 'logfile' is set to 'stdout' in the configuration file
	* it will not log at all. */
	if ((fd = open("/dev/null", O_RDWR, 0)) != -1) {
		dup2(fd, STDIN_FILENO);
		dup2(fd, STDOUT_FILENO);
		dup2(fd, STDERR_FILENO);
{'fraction_non_alphanumeric': 0.14576929299466107, 'fraction_numerical': 0.014884322925093027, 'mean_word_length': 3.8524332810047097, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5468333', 'n_tokens_mistral': 2365, 'n_tokens_neox': 2219, 'n_words': 687}