Ejemplo n.º 1
0
int main(int argc, char **argv)
{
	int option_index;
	int c;
	char filename[4096];
	char workload[4096] = {0,};
	int  iterations = 1, auto_tune = 0;

	set_new_handler(out_of_memory);

	setlocale (LC_ALL, "");
	bindtextdomain (PACKAGE, LOCALEDIR);
	textdomain (PACKAGE);

	while (1) { /* parse commandline options */
		c = getopt_long (argc, argv, "ch:C:i:t:uVw:q", long_options, &option_index);
		/* Detect the end of the options. */
		if (c == -1)
			break;

		switch (c) {
			case 'V':
				print_version();
				exit(0);
				break;

			case 'e': /* Extech power analyzer support */
				checkroot();
				extech_power_meter(optarg ? optarg : "/dev/ttyUSB0");
				break;
			case 'u':
				print_usage();
				exit(0);
				break;
			case 'a':
				auto_tune = 1;
				leave_powertop = 1;
				break;
			case 'c':
				powertop_init();
				calibrate();
				break;

			case 'h': /* html report */
				reporttype = REPORT_HTML;
				sprintf(filename, "%s", optarg ? optarg : "powertop.html" );
				break;

			case 't':
				time_out = (optarg ? atoi(optarg) : 20);
				break;

			case 'i':
				iterations = (optarg ? atoi(optarg) : 1);
				break;

			case 'w': /* measure workload */
				sprintf(workload, "%s", optarg ? optarg :'\0' );
				break;
			case 'q':
				if(freopen("/dev/null", "a", stderr))
					fprintf(stderr, _("Quite mode failed!\n"));
				break;

			case 'C': /* csv report*/
				reporttype = REPORT_CSV;
				sprintf(filename, "%s", optarg ? optarg : "powertop.csv");
				break;
			case '?': /* Unknown option */
				/* getopt_long already printed an error message. */
				exit(0);
				break;
		}
	}

	powertop_init();

	if (reporttype != REPORT_OFF)
		make_report(time_out, workload, iterations, filename);

	if (debug_learning)
		printf("Learning debugging enabled\n");

	learn_parameters(250, 0);
	save_parameters("saved_parameters.powertop");


	if (debug_learning) {
	        learn_parameters(1000, 1);
		dump_parameter_bundle();
		end_pci_access();
		exit(0);
	}
	init_display();
	initialize_tuning();
	/* first one is short to not let the user wait too long */
	one_measurement(1, NULL);

	if (!auto_tune) {
		tuning_update_display();
		show_tab(0);
	} else {
		auto_toggle_tuning();
	}

	while (!leave_powertop) {
		show_cur_tab();
		one_measurement(time_out, NULL);
		learn_parameters(15, 0);
	}
	endwin();
	printf("%s\n", _("Leaving PowerTOP"));

	end_process_data();
	clear_process_data();
	end_cpu_data();
	clear_cpu_data();

	save_all_results("saved_results.powertop");
	save_parameters("saved_parameters.powertop");
	learn_parameters(500, 0);
	save_parameters("saved_parameters.powertop");
	end_pci_access();
	clear_tuning();
	reset_display();

	clean_shutdown();

	return 0;
}
Ejemplo n.º 2
0
int main (int argc, char* argv[])
{
  // Create our single-loop for this single-thread application
  EV_P;
  pthread_attr_t attr;
  int thread_status;
  struct evn_server* server;
  char socket_address[256];
  EV_A = ev_default_loop(0);


  // Set default options, then parse new arguments to update the settings
  dummy_settings_set_presets(&dummy_settings);
  argp_parse (&argp, argc, argv, 0, 0, &dummy_settings);
  // TODO separate worker settings from daemon settings
  // (i.e. redirect)

  if (true == redirect)
  {
    redirect_output();
  }

  if (0)
  {
    struct ev_periodic every_few_seconds;

    // To be sure that we aren't actually blocking
    ev_periodic_init(&every_few_seconds, test_process_is_not_blocked, 0, 1, 0);
    ev_periodic_start(EV_A_ &every_few_seconds);
  }

  // Set the priority of this whole process higher (requires root)
  setpriority(PRIO_PROCESS, 0, -13); // -15

  // initialize the values of the struct that we will be giving to the new thread
  thread_control.dummy_settings = &dummy_settings;
  thread_control.buffer_head = 0;
  thread_control.buffer_count = 0;
  thread_control.EV_A = ev_loop_new(EVFLAG_AUTO);

  pthread_attr_init(&attr);
  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  // Initialize the thread that will manage the DUMMY_WORKER
  thread_status = pthread_create(&dummy_worker_pthread, &attr, dummy_worker_thread, (void *)(&thread_control));
  if (0 != thread_status)
  {
    fprintf(stderr, "thread creation failed with errno %d (%s)\n", thread_status, strerror(thread_status));
    exit(EXIT_FAILURE);
  }
  pthread_attr_destroy(&attr);


  // Create unix socket in non-blocking fashion
  snprintf(socket_address, sizeof(socket_address), DUMMYD_SOCK, (int)getuid());
  unlink(socket_address);
  server = evn_server_create(EV_A_ server_on_connection);
  server->on_connection = server_on_connection;
  evn_server_listen(server, 0, socket_address);

  // Run our loop, until we recieve the QUIT, TERM or INT signals, or an 'x' over the socket.
  puts("[Daemon] Looping.\n");
  ev_loop(EV_A_ 0);

  // Cleanup if `unloop` is ever called
  clean_shutdown(EV_A_ 0);
  return 0;
}
Ejemplo n.º 3
0
Archivo: queue.c Proyecto: tomrf/ponies
void* queue_mgr(void *arg)
{
  pthread_t             *my_id;

  list_t                *queue_list;
  node_t                *node;

  queue_node_t          *queue_node;

  int                   new_entries;

  int                   jobs_downloading;
  int                   jobs_waiting;
  int                   jobs_starting;
  int                   jobs_finished;

  int                   did_close_connections = 1;

  my_id = arg;

  queue_list = list_create();

  /* load queue state from db */
  sqw_queue_repopulate(queue_list);

  for (;;) {
    new_entries = queue_scan_dir(queue_list, global_opts.nzb_queuedir_drop);

    jobs_waiting = queue_count_status(queue_list, QSTAT_WAITING);
    jobs_starting = queue_count_status(queue_list, QSTAT_STARTING);
    jobs_downloading = queue_count_status(queue_list, QSTAT_DOWNLOADING);
    jobs_finished = queue_count_status(queue_list, QSTAT_FINISHED);

    if (new_entries > 0) {
        msg_send_to_type((int)my_id, TT_UI_MGR, NZB_STATUS,
            (void *)queue_list, NULL, 1);
        DEBUG(1, "added %d new entries to queue; %d waiting, %d downloading",
            new_entries, jobs_waiting, jobs_downloading);
    }


    //XXX: for profiling
    if (jobs_waiting + jobs_starting + jobs_downloading == 0) {
        if (global_opts.nntp_logoff_when_idle == TRUE && !did_close_connections) {
            did_close_connections = 1;
            free_all_connections();
        }
        else if (!global_opts.nntp_logoff_when_idle)
            clean_shutdown();
    }
    else
      did_close_connections = 0;

    if ((jobs_downloading + jobs_starting < global_opts.max_jobs) && jobs_waiting > 0) {
      list_lock(queue_list);

      node = queue_list->head;

      for (;;) {
        queue_node = node->data;
        if (queue_node->queue_status == QSTAT_WAITING) {
          DEBUG(3, "sending NZB_START_DL request to TT_DL_MGR for queue_node %p", queue_node);
          msg_send_to_type((int)my_id, TT_DL_MGR, NZB_START_DL, queue_node, NULL, 1);
          list_unlock(queue_list);
          break;
        }

        if ((node = node->next) == NULL) {
          list_unlock(queue_list);
          break;
        }

      } /* for */

    } /* send NZB_START_DL request to download mgr*/


    //printf("queue_mgr()\n");
    sleep(3);
  }


  return NULL;
}
Ejemplo n.º 4
0
/*
 * Create a socket for all incoming requests on specified port.
 * If info.myhostname is NULL, bind it to INADDR_ANY (all available interfaces).
 * If info.myhostname is not NULL, resolv it (if needed), and bind to that.
 * Return the socket for bound socket, or INVALID_SOCKET if failed.
 * Assert Class: 3
 */
SOCKET sock_get_server_socket(const int port)
{
	struct sockaddr_in sin;
	int sin_len, error;
	SOCKET sockfd;

	if (port < 0) {
		write_log(LOG_DEFAULT,
			  "ERROR: Invalid port number %d. Cannot listen for requests, this is bad!",
			  port);
		return INVALID_SOCKET;
	}

	xa_debug (2, "DEBUG: Getting socket for port %d", port);

	/*
	 * get socket descriptor 
	 */
	sockfd = sock_socket (AF_INET, SOCK_STREAM, 0);
	if (sockfd == INVALID_SOCKET)
		return INVALID_SOCKET;

	/*
	 * Setup socket 
	 */
#ifdef HAVE_SETSOCKOPT
	{
		int tmp = 1;

		if (setsockopt
		    (sockfd, SOL_SOCKET, SO_REUSEADDR, (const void *) &tmp,
		     sizeof (tmp)) != 0)
			write_log(LOG_DEFAULT,
				  "ERROR: setsockopt() failed to set SO_REUSEADDR flag. (mostly harmless)");
	}
#endif

	/*
	 * setup sockaddr structure 
	 */
	sin_len = sizeof (sin);
	memset(&sin, 0, sin_len);
	sin.sin_family = AF_INET;

	if (info.myhostname != NULL && info.myhostname[0]) {
		if (isdigit((int) info.myhostname[0])
		    && isdigit((int) info.
			       myhostname[ice_strlen(info.myhostname) - 1])) {
			if (inet_aton
			    (info.myhostname,
			     (struct in_addr *) &localaddr) == 0) {
				write_log(LOG_DEFAULT,
					  "ERROR: Invalid ip number %s, will die now",
					  info.myhostname);
				clean_shutdown(&info);
			}
			sin.sin_addr.s_addr = localaddr.s_addr;
		} else {
			struct hostent *hostinfoptr, hostinfo;
			char buf[BUFSIZE];
			int error;

			hostinfoptr =
				ice_gethostbyname(info.myhostname, &hostinfo,
						  buf, BUFSIZE, &error);
			if (hostinfoptr == NULL) {
				write_log(LOG_DEFAULT,
					  "Unknown host %s, that's it for me!",
					  info.myhostname);
				ice_clean_hostent();
				clean_shutdown(&info);
			}
			sin.sin_addr.s_addr = localaddr.s_addr;
			memcpy((void *) &localaddr, hostinfoptr->h_addr,
			       sizeof (localaddr));
			ice_clean_hostent();
		}
	} else {
		sin.sin_addr.s_addr = htonl(INADDR_ANY);
	}

	sin.sin_port = htons(port);

	/*
	 * bind socket to port 
	 */
	error = bind(sockfd, (struct sockaddr *) &sin, sin_len);
	if (error == SOCKET_ERROR) {
		write_log(LOG_DEFAULT,
			  "Bind to socket on port %d failed. Shutting down now.",
			  port);
		clean_shutdown(&info);
	}

	return sockfd;
}