예제 #1
0
float
get_cpu_load_atomic(void)
{
    char buf[MAXLINE];
    char c;
    char **list;
    int i, statfd;
    unsigned long total_time, idle_time;
    float idle_pct;

    while ((statfd = open("/proc/stat", O_RDONLY)),statfd == -1 && errno == EINTR)
        ;
    if (statfd == -1) {
        perror("Failed to read /proc/stat");
        return -1;
    }

    i = 0;
    while ((read(statfd, &c, 1) != -1) && c != '\n')
        buf[i++] = c;
    buf[i] = 0;

    list = string_to_list(buf, " ");

    total_time = 0;
    idle_time = atol(list[4]);
    for (i = 1; list[i] != 0; i++)
        total_time += atol(list[i]);
    
    idle_pct = ((float)idle_time/(float)total_time) * 100;
    destroy_list((void **)list);

    return idle_pct;
}
예제 #2
0
파일: main.c 프로젝트: b3h3moth/L-C
int main(void)
{
   char   line[MAXLINE];
   LINK   hp;                /* hp = head pointer */
   LINK   p;

   printf("%s",
      "---\n"
      "Type in a line of text: ");
   assert(fgets(line, MAXLINE, stdin) != NULL);
   hp = string_to_list(line);
   printf("%s%s%s",
      "\n"
      "Here is the line you type in:\n"
      "\n"
      "   ", line,
      "\n"
      "and here is what we get running down the linked list:\n"
      "\n"
      "   ");
   for (p = hp; p != NULL; p = p -> next)
      putchar(p -> d);
   printf("\n");
   return 0;
}
예제 #3
0
int main(void)
{
	char input[N];
	LINK h=NULL;
	printf("문자열 입력 : ");
	scanf("%s", input);
	h=string_to_list(input);
	printf("변환 리스트 결과 : \n");
	print_list(h);
	return 0;
}
예제 #4
0
LINK string_to_list(char s[]) 
{
	LINK head = NULL;

	if (s[0]=='\0')
		return NULL;
	else
	{
		head=malloc(sizeof(ELEMENT));
		head->d=s[0];
		head->next=string_to_list(s+1);
		return head;
	}
}
예제 #5
0
krb5_error_code KRB5_LIB_FUNCTION
krb5_set_default_realm(krb5_context context,
		       const char *realm)
{
    krb5_error_code ret = 0;
    krb5_realm *realms = NULL;

    if (realm == NULL) {
	realms = krb5_config_get_strings (context, NULL,
					  "libdefaults",
					  "default_realm",
					  NULL);
	if (realms == NULL)
	    ret = krb5_get_host_realm(context, NULL, &realms);
    } else {
	ret = string_to_list (context, realm, &realms);
    }
    if (ret)
	return ret;
    krb5_free_host_realm (context, context->default_realms);
    context->default_realms = realms;
    return 0;
}
예제 #6
0
파일: main.c 프로젝트: Firstyear/ds
void main(int argc, char** argv)
{
    int index = 0, numThreads, numDead = 0;
    int ch;
    SearchThread **threads;

    while ((ch = getopt(argc, argv, "j:i:h:s:f:p:t:D:w:n:A:bvlyqmcdu")) != EOF)
	switch (ch) {
	case 'h':
	    hostname = optarg;
	    break;
	case 's':
	    suffix = optarg;
	    break;
	case 'f':
	    filter = optarg;
	    break;
	case 'i':
	    nameFile = optarg;
	    break;
	case 'D':
	    bindDN = optarg;
	    break;
	case 'w':
	    bindPW = optarg;
	    break;
	case 'A':
	    attrList = optarg;
	    break;
	case 'p':
	    port = atoi(optarg);	
	    break;
	case 'b':			
	    doBind = 1;
	    break;
	case 'u':			
	    noUnBind = 1;
	    break;
	case 'n':			
	    numeric = atoi(optarg);
	    break;
	case 't':		
	    threadCount = atoi(optarg);
	    break;
	case 'j':		
	    sampleInterval = atoi(optarg) * 1000;
	    break;
	case 'v':
	    verbose = 1;
	    break;
	case 'q':
	    quiet = 1;
	    break;
	case 'l':
	    logging = 1;
	    break;
	case 'y':
	    noDelay = 1;
	    break;
	case 'm':
	    opType = op_modify;
	    break;
	case 'd':
	    opType = op_delete;
	    break;
	case 'c':
	    opType = op_compare;
	    break;
	case '?':
	    usage();
	    exit(1);
	    break;
	default:
	    break;
	}
    argc -= optind;
    argv += optind;

    PR_Init(PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 0);

    ntable = nt_new(0);
    if (nameFile) {
	if (!nt_load(ntable, nameFile)) {
	    printf("Failed to read name table\n");
	    exit(1);
	}
    }

    if (attrList)
	attrToReturn = string_to_list(attrList);

    /* a "vector" */
    threads = (SearchThread **)malloc(threadCount * sizeof(SearchThread *));

    while (threadCount--) {
	SearchThread *st;
	PRThread *thr;

	st = st_new();
	thr = PR_CreateThread(PR_SYSTEM_THREAD, search_start,
			      (void *)st, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
			      PR_JOINABLE_THREAD, 0);
	st_setThread(st, thr, index+1);
	threads[index++] = st;
    }
    numThreads = index;

    printf("rsearch: %d threads launched.\n\n", numThreads);

    while (numThreads != numDead) {
	int x;

	PR_Sleep(PR_MillisecondsToInterval(sampleInterval));

	/* now check for deadies */
	for (x = 0; x < numThreads; x++) {
	    if (!st_alive(threads[x])) {
		int y;
		PRThread *tid;

		printf("T%d DEAD.\n", st_getThread(threads[x], &tid));
		PR_JoinThread(tid);
		for (y = x+1; y < numThreads; y++)
		    threads[y-1] = threads[y];
		numThreads--;
		numDead++;
		x--;
	    }
	}

	/* print out stats */
	if (!quiet) {
	    PRUint32 total = 0;

	    for (x = 0; x < numThreads; x++) {
		PRUint32 count, min, max;

		st_getCountMinMax(threads[x], &count, &min, &max);
		total += count;
		printf("T%d min=%4ums, max=%4ums, count = %u\n",
		       st_getThread(threads[x], NULL), min, max, count);
	    }
	    if (numThreads > 1)
		printf("Average rate = %.2f\n", 
		       (double)total/(double)numThreads);
	}
	/* watchdogs were reset when we fetched the min/max counters */
    }

    printf("All threads died. (?)\n");
    exit(1);
}
예제 #7
0
파일: rsearch.c 프로젝트: Firstyear/ds
int main(int argc, char** argv)
{
    int index = 0, numThreads, numDead = 0;
    int ch;
    int lifeTime;
    SearchThread **threads;
    PRUint32 total;
    double rate, val, cumrate;
    double sumVal;
    int counter;

    if (argc == 1) {
	usage();
	exit(1); 
    }

    while ((ch = getopt(argc, argv, 
		"U:W:B:a:j:i:h:s:f:p:o:t:T:D:w:n:A:S:C:R:bvlyqmMcduNLHx?V"))
	   != EOF)
	switch (ch) {
	case 'h':
	    hostname = optarg;
	    break;
	case 's':
	    suffix = optarg;
	    break;
	case 'f':
	    filter = optarg;
	    break;
	case 'i':
	    nameFile = optarg;
	    break;
	case 'B':
	    if (optarg[0] == '?') {
		usage_B();
		exit(1);
	    }
	    searchDatFile = optarg;
	    break;
	case 'D':
	    bindDN = optarg;
	    break;
	case 'w':
	    bindPW = optarg;
	    break;
	case 'A':
		if (!attrFile)
	    	attrList = optarg;
		else 
			usage();
	    break;
	case 'p':
	    port = atoi(optarg);	
	    break;
	case 'S':
	    myScope = atoi(optarg);
	    if (myScope < LDAP_SCOPE_BASE || myScope > LDAP_SCOPE_SUBTREE)
		myScope = LDAP_SCOPE_SUBTREE;
	    break;
	case 'C':
	    countLimit = atoi(optarg);
	    break;
	case 'b':			
	    doBind = 1;
	    break;
	case 'u':			
	    noUnBind = 1;
	    break;
	case 'L':			
	    setLinger = 1;
	    break;
	case 'n':			
	    numeric = atoi(optarg);
	    break;
	case 'o':		
	    searchTimelimit = atoi(optarg);
	    break;
	case 't':		
	    threadCount = atoi(optarg);
	    break;
	case 'j':		
	    sampleInterval = atoi(optarg) * 1000;
	    break;
	case 'v':
	    verbose = 1;
	    break;
	case 'q':
	    quiet = 1;
	    break;
	case 'l':
	    logging = 1;
	    break;
	case 'y':
	    noDelay = 1;
	    break;
	case 'm':
	    opType = op_modify;
	    break;
	case 'M':
	    opType = op_idxmodify;
	    break;
	case 'd':
	    opType = op_delete;
	    break;
	case 'c':
	    opType = op_compare;
	    break;
	case 'N':
	    noOp = 1;
	    doBind = 1;	/* no use w/o this */
	    break;
	case 'T':
	    timeLimit = atoi(optarg);
	    break;
	case 'V':
	    showRunningAvg = 1;
	    break;
	case 'R':
	    reconnect = atoi(optarg);
	    break;
	case 'x':
	    useBFile = 1;
	    break;
	case 'W':
		userPW = optarg;
		break;
	case 'U':
		uidFilter = optarg;
		break;
	case 'a':
	    if (optarg[0] == '?') {
		usage_A();
		exit(1);
	    }
		if (!attrList)
	    	attrFile = optarg;
		else
			usage();
		break;
	case '?':
	case 'H':
	default :
		usage();
	}

    if ( !suffix || !filter || !bindDN || !bindPW ) {
	printf("rsearch: missing option\n");
	usage();
    }

    if ( timeLimit < 0 || threadCount <= 0 || sampleInterval <= 0 ) {
	printf("rsearch: invalid option value\n");
	usage();
    }
    argc -= optind;
    argv += optind;

	if (uidFilter && NULL == strstr(uidFilter, "%s")) {
		printf("rsearch: invalid UID filter - must contain %%s, eg, (uid=%%s)\n");
		usage();
	}

    PR_Init(PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 0);


    if (nameFile) {
        ntable = nt_new(0);
        if (!nt_load(ntable, nameFile)) {
            printf("Failed to read name table\n");
            exit(1);
        }
    }

	attrTable = nt_new(0);
	if (attrFile) 
	{
		if (!nt_load(attrTable , attrFile)) 
		{
			printf ("Failed to read attr name table\n");
			exit(1);
		}
	}

    sdattable = sdt_new(0);
    if (searchDatFile) {
	if (!sdt_load(sdattable, searchDatFile)) {
	    printf("Failed to read search data table: %s\n", searchDatFile);
	    exit(1);
	}
    }

    if (attrList)
	attrToReturn = string_to_list(attrList);
	

    /* a "vector" */
    threads = (SearchThread **)malloc(threadCount * sizeof(SearchThread *));

    while (threadCount--) {
	SearchThread *st;
	PRThread *thr;

	st = st_new();
	thr = PR_CreateThread(PR_SYSTEM_THREAD, search_start,
			      (void *)st, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
			      PR_JOINABLE_THREAD, 0);
	st_setThread(st, thr, index+1);
	threads[index++] = st;
    }
    numThreads = index;

    printf("rsearch: %d threads launched.\n\n", numThreads);

    lifeTime = 0;
    counter = 0;
    sumVal = 0;
    cumrate = 0.0;
    while (numThreads) {
	int x, alive;

	PR_Sleep(PR_MillisecondsToInterval(sampleInterval));

	counter++;
	lifeTime += sampleInterval/1000;
	/* now check for deadies */
	for (x = 0; x < numThreads; x++) {
	    alive = st_alive(threads[x]);
	    if (alive < 1) {
		int limit = -1 * (searchTimelimit>timeLimit?searchTimelimit:timeLimit + 40) * 1000 / sampleInterval;
		int y;
		PRThread *tid;

		printf("T%d no heartbeat", st_getThread(threads[x], &tid));
		if (alive <= limit) {
		    printf(" -- Dead thread being reaped.\n");
		    PR_JoinThread(tid);
		    for (y = x+1; y < numThreads; y++)
			threads[y-1] = threads[y];
		    numThreads--;
		    numDead++;
		    x--;
		}
		else
		   printf(" (waiting)\n");
	    }
	}

	/* print out stats */
	total = 0;
	for (x = 0; x < numThreads; x++) {
	    PRUint32 count, min, max;

	    st_getCountMinMax(threads[x], &count, &min, &max);
	    total += count;
	    if (!quiet && verbose)
		printf("T%d min=%4ums, max=%4ums, count = %u\n",
			   st_getThread(threads[x], NULL), min, max, count);
	}
	rate = (double)total / (double)numThreads;
	val = 1000.0 * (double)total / (double)sampleInterval;
	cumrate += rate;
	if ((numThreads > 1) || (!verbose)) {
	    if (!quiet) {
			char tbuf[18];
			struct tm* now;
			time_t lt;

			time(&lt);
			now = localtime(&lt);
			strftime(tbuf, sizeof(tbuf), "%Y%m%d %H:%M:%S", now);
		if (showRunningAvg)
		    printf("%s - Rate: %7.2f/thr (cumul rate: %7.2f/thr)\n",
			   tbuf, rate, cumrate/(double)counter);
		else
		    printf("%s - Rate: %7.2f/thr (%6.2f/sec =%7.4fms/op), "
			   "total:%6u (%d thr)\n",
			   tbuf, rate, val, (double)1000.0/val, total, numThreads);
	    }
	}
	if (countLimit && (counter >= countLimit)) {
	    printf("Thank you, and good night.\n");
	    exit(0);
	}
	if (timeLimit && (lifeTime >= timeLimit)) {
	    double tmpv = (val + sumVal)/counter;
	    if (verbose)
		printf("%d sec >= %d\n", lifeTime, timeLimit);
	    printf("Final Average rate: "
		            "%6.2f/sec = %6.4fmsec/op, total:%6u\n",
			    tmpv,
			    (double)1000.0/tmpv,
			    total);
	    exit(0);
	}
	sumVal += val;
	/* watchdogs were reset when we fetched the min/max counters */
    }

    printf("All threads died. (?)\n");
    exit(1);
}