예제 #1
0
파일: io_ncurses.c 프로젝트: abstrakct/gt2
bool blocks_light(void *level, int y, int x)
{
        level_t *l = (level_t *) level;

        if(hasbit(l->c[y][x].flags, CF_HAS_DOOR_CLOSED))
                return true;

        if(l->c[y][x].type == AREA_FOREST || l->c[y][x].type == AREA_CITY || l->c[y][x].type == AREA_VILLAGE) {    // trees and houses can be "see through" (e.g. if they are small)
                if(perc(20))
                        return false;
                else
                        return true;
        }

        switch(l->c[y][x].type) {
                case AREA_NOTHING:
                case AREA_WALL:
                case DNG_WALL:
                       return true;
                default:       
                       return false;
        }

        // shouldn't be reached...
        return false;
}
예제 #2
0
파일: monsters.c 프로젝트: abstrakct/gt2
/**
 * @brief Make a move for a specific monster. Heal the monster if appropriate.
 *
 * @param m Monster to move.
 *
 */
void move_monster(monster_t *m)
{
        int i;

        if(!m) {
                fprintf(stderr, "DEBUG: %s:%d - no such monster!\n", __FILE__, __LINE__);
                return;
        }

        if(hasbit(m->flags, MF_ISDEAD)) {
                //fprintf(stderr, "DEBUG: %s:%d - monster is dead!\n", __FILE__, __LINE__);
                return;
        }

        if(hasbit(m->flags, MF_SLEEPING)) {
                if(actor_in_lineofsight(m, player))
                        clearbit(m->flags, MF_SLEEPING);
        }

        if(!hasbit(m->flags, MF_SLEEPING)) {
                hostile_ai(m);
                if(m->hp < m->maxhp) {
                        i = 17 - m->attr.phy;
                        if(i <= 0)
                                i = 1;
                        if(!game->tick % i) {
                                if(perc(40+m->attr.phy)) {
                                        int j;

                                        j = ability_modifier(m->attr.phy);
                                        if(j < 1)
                                                j = 1;
                                        heal_monster(m, ri(1, j));
                                }
                        }
                }
        }

        schedule_monster(m);
}
예제 #3
0
파일: monsters.c 프로젝트: abstrakct/gt2
/**
 * @brief Kill a monster.
 *
 * @param level Pointer to the level where the killing takes place.
 * @param m Pointer to the unfortunate victim.
 * @param killer Pointer to the entity which did the killing.
 */
void kill_monster(void *level, monster_t *m, actor_t *killer)
{
        int i;
        level_t *l;

        l = (level_t *) level;

        if(l->c[m->y][m->x].monster == m) {
                // we probably should free/remove dead monsters, but something keeps going wrong, cheap cop-out:
                // also, this has it's advantages later (can be used for listing killed monsters).
                setbit(l->c[m->y][m->x].monster->flags, MF_ISDEAD);
                l->c[m->y][m->x].monster = NULL;
                if(killer == player) {
                        player->kills++;
                }

                // and a chance of the monster dropping some or all of its inventory
                if(m->inventory) {
                        if(perc(75)) {
                                for(i=0;i<52;i++) {   // most likely monster won't have 52 items, but you never know...
                                        if(m->inventory->object[i]) {
                                                drop(m, m->inventory->object[i]);
                                        }
                                }

                                if(m->inventory->gold) {
                                        l->c[m->y][m->x].inventory->gold += m->inventory->gold;
                                        m->inventory->gold = 0;
                                }
                                        
                        }
                }
        } else {
                gtprintf("monster's x&y doesn't correspond to cell?");
        }
}
예제 #4
0
void fincore(char* path, 
             struct fincore_result *result ) {

    int fd;
    struct stat file_stat;
    void *file_mmap;

    // vector result from mincore
    unsigned char *mincore_vec;

    // default page size of this OS
    size_t page_size = getpagesize();
    size_t page_index;

    int i; 

    // the number of pages that we have detected that are cached 
    size_t cached = 0;

    // the number of pages that we have printed 
    size_t printed = 0;

    //the total number of pages we're working with
    size_t total_pages;

    //the oldest page in the cache or -1 if it isn't in the cache.
    off_t min_cached_page = -1 ;

    // by default the cached size is zero so initialize this member.
    result->cached_size = 0;

    //FIXME: make this sizeof(regions)
    if ( regions == NULL )
        regions = calloc( nr_regions , sizeof(long) ) ;

    if ( region_percs == NULL )
        region_percs = calloc( nr_regions , sizeof(double) ) ;

    if ( regions == NULL || region_percs == NULL ) {
        perror( "Could not allocate memory" );
        goto cleanup;      
    }

    for( i = 0; i < nr_regions; ++i ) {
        regions[i] = (long)0;
        region_percs[i] = (double)0;
    }

    fd = open( path, O_RDONLY );

    if ( fd == -1 ) {
        
        //FIXME: migrate this code to a decated function for dealing with errors.
        char buff[1024];
        sprintf( buff, "Could not open file: %s", path );
        perror( buff );

        return;
    }

    if ( fstat( fd, &file_stat ) < 0 ) {

        char buff[1024];
        sprintf( buff, "Could not stat file: %s", path );
        perror( buff );

        goto cleanup;

    }

    if ( file_stat.st_size == 0 ) {
        // this file could not have been cached as it's empty so anything we
        //would do is pointless.
        goto cleanup;
    }

    file_mmap = mmap((void *)0, file_stat.st_size, PROT_NONE, MAP_SHARED, fd, 0 );
    
    if ( file_mmap == MAP_FAILED ) {

        char buff[1024];
        sprintf( buff, "Could not mmap file: %s", path );
        perror( buff );
        goto cleanup;      
    }

    size_t calloc_size = (file_stat.st_size+page_size-1)/page_size;

    mincore_vec = calloc(1, calloc_size);

    if ( mincore_vec == NULL ) {
        perror( "Could not calloc" );
        exit( 1 );
    }

    if ( mincore(file_mmap, file_stat.st_size, mincore_vec) != 0 ) {
        char buff[1024];
        sprintf( buff, "%s: Could not call mincore on file", path );
        perror( buff );
        exit( 1 );
    }

    total_pages = (int)ceil( (double)file_stat.st_size / (double)page_size );

    int region_ptr = (int)((total_pages - 1) / nr_regions);

    for (page_index = 0; page_index <= file_stat.st_size/page_size; page_index++) {

        if (mincore_vec[page_index]&1) {

            ++cached;

            if ( min_cached_page == -1 || page_index < min_cached_page ) {
                min_cached_page = page_index;
            }

            if ( arg_pages ) {
                printf("%lu ", (unsigned long)page_index);
                ++printed;
            }

            if ( region_ptr > 0 ) {
                int region = (int)(page_index / region_ptr );
                ++regions[region];
            }

        }

    }

    if ( printed ) printf("\n");

    double cached_perc = 100 * (cached / (double)total_pages); 

    size_t cached_size = (long)cached * (long)page_size;

    if ( arg_min_cached_size > 0 && cached_size <= arg_min_cached_size ) {
        goto cleanup;
    }

    if ( arg_min_cached_perc > 0 && cached_perc <= arg_min_cached_perc ) {
        goto cleanup;
    }

    if ( arg_min_size > 0 && file_stat.st_size <= arg_min_size ) {
        goto cleanup;
    }

    if ( arg_only_cached == 0 || cached > 0 ) {

        if ( arg_graph ) {
            _show_headers();
        }

        if ( arg_vertical ) {

            printf( "%s\n", path );
            printf( "size: %'ld\n", file_stat.st_size );
            printf( "total_pages: %'ld\n", total_pages );
            printf( "min_cached_page: %'ld\n", min_cached_page );
            printf( "cached: %'ld\n", cached );
            printf( "cached_size: %'ld\n", cached_size );
            printf( "cached_perc: %.2f\n", cached_perc );

        } else { 

            printf( DATA_FORMAT, 
                    path, 
                    file_stat.st_size , 
                    total_pages , 
                    min_cached_page,
                    cached ,  
                    cached_size , 
                    cached_perc );

        }

        for( i = 0 ; i < nr_regions; ++i ) {
            region_percs[i] = perc(regions[i], region_ptr );
        }

        if ( arg_graph && total_pages > nr_regions ) {
            graph( region_percs, nr_regions );
        }

    }

    // update structure members when done.
    result->cached_size = cached_size;

 cleanup:

    if ( mincore_vec != NULL ) {
        free(mincore_vec);
        mincore_vec = NULL;
    }

    if ( file_mmap != MAP_FAILED )
        munmap(file_mmap, file_stat.st_size);

    if ( fd != -1 )
        close(fd);

    return;

}
예제 #5
0
static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg *msg,
					 struct nlattr **attrs)
{
	char *name;
	struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
	struct nlattr *prop[TIPC_NLA_PROP_MAX + 1];
	struct nlattr *stats[TIPC_NLA_STATS_MAX + 1];

	nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK], NULL);

	nla_parse_nested(prop, TIPC_NLA_PROP_MAX, link[TIPC_NLA_LINK_PROP],
			 NULL);

	nla_parse_nested(stats, TIPC_NLA_STATS_MAX, link[TIPC_NLA_LINK_STATS],
			 NULL);

	name = (char *)TLV_DATA(msg->req);
	if (strcmp(name, nla_data(link[TIPC_NLA_LINK_NAME])) != 0)
		return 0;

	tipc_tlv_sprintf(msg->rep, "\nLink <%s>\n",
			 nla_data(link[TIPC_NLA_LINK_NAME]));

	if (link[TIPC_NLA_LINK_BROADCAST]) {
		__fill_bc_link_stat(msg, prop, stats);
		return 0;
	}

	if (link[TIPC_NLA_LINK_ACTIVE])
		tipc_tlv_sprintf(msg->rep, "  ACTIVE");
	else if (link[TIPC_NLA_LINK_UP])
		tipc_tlv_sprintf(msg->rep, "  STANDBY");
	else
		tipc_tlv_sprintf(msg->rep, "  DEFUNCT");

	tipc_tlv_sprintf(msg->rep, "  MTU:%u  Priority:%u",
			 nla_get_u32(link[TIPC_NLA_LINK_MTU]),
			 nla_get_u32(prop[TIPC_NLA_PROP_PRIO]));

	tipc_tlv_sprintf(msg->rep, "  Tolerance:%u ms  Window:%u packets\n",
			 nla_get_u32(prop[TIPC_NLA_PROP_TOL]),
			 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));

	tipc_tlv_sprintf(msg->rep,
			 "  RX packets:%u fragments:%u/%u bundles:%u/%u\n",
			 nla_get_u32(link[TIPC_NLA_LINK_RX]) -
			 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
			 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
			 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
			 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
			 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));

	tipc_tlv_sprintf(msg->rep,
			 "  TX packets:%u fragments:%u/%u bundles:%u/%u\n",
			 nla_get_u32(link[TIPC_NLA_LINK_TX]) -
			 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
			 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
			 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
			 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
			 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));

	tipc_tlv_sprintf(msg->rep,
			 "  TX profile sample:%u packets  average:%u octets\n",
			 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_CNT]),
			 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_TOT]) /
			 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT]));

	tipc_tlv_sprintf(msg->rep,
			 "  0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% ",
			 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P0]),
			      nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
			 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P1]),
			      nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
			 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P2]),
			      nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
			 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P3]),
			      nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));

	tipc_tlv_sprintf(msg->rep, "-16384:%u%% -32768:%u%% -66000:%u%%\n",
			 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P4]),
			      nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
			 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P5]),
			      nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
			 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P6]),
			      nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));

	tipc_tlv_sprintf(msg->rep,
			 "  RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
			 nla_get_u32(stats[TIPC_NLA_STATS_RX_STATES]),
			 nla_get_u32(stats[TIPC_NLA_STATS_RX_PROBES]),
			 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
			 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
			 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));

	tipc_tlv_sprintf(msg->rep,
			 "  TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
			 nla_get_u32(stats[TIPC_NLA_STATS_TX_STATES]),
			 nla_get_u32(stats[TIPC_NLA_STATS_TX_PROBES]),
			 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
			 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
			 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));

	tipc_tlv_sprintf(msg->rep,
			 "  Congestion link:%u  Send queue max:%u avg:%u",
			 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
			 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
			 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));

	return 0;
}
예제 #6
0
파일: link.c 프로젝트: eworm-de/iproute2
static int _show_link_stat(const char *name, struct nlattr *attrs[],
			   struct nlattr *prop[], struct nlattr *stats[])
{
	uint32_t proft;

	open_json_object(NULL);

	print_string(PRINT_ANY, "link", "\nLink <%s>\n", name);
	print_string(PRINT_JSON, "state", "", NULL);
	open_json_array(PRINT_JSON, NULL);
	if (attrs[TIPC_NLA_LINK_ACTIVE])
		print_string(PRINT_ANY, NULL, "  %s", "ACTIVE");
	else if (attrs[TIPC_NLA_LINK_UP])
		print_string(PRINT_ANY, NULL, "  %s", "STANDBY");
	else
		print_string(PRINT_ANY, NULL, "  %s", "DEFUNCT");
	close_json_array(PRINT_JSON, NULL);

	print_uint(PRINT_ANY, "mtu", "  MTU:%u",
			   mnl_attr_get_u32(attrs[TIPC_NLA_LINK_MTU]));
	print_uint(PRINT_ANY, PRIORITY_STR, "  Priority:%u",
			   mnl_attr_get_u32(prop[TIPC_NLA_PROP_PRIO]));
	print_uint(PRINT_ANY, TOLERANCE_STR, "  Tolerance:%u ms",
			   mnl_attr_get_u32(prop[TIPC_NLA_PROP_TOL]));
	print_uint(PRINT_ANY, WINDOW_STR, "  Window:%u packets\n",
			   mnl_attr_get_u32(prop[TIPC_NLA_PROP_WIN]));

	open_json_object("rx packets");
	print_uint(PRINT_ANY, "rx packets", "  RX packets:%u",
			   mnl_attr_get_u32(attrs[TIPC_NLA_LINK_RX]) -
			   mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_INFO]));
	print_uint(PRINT_ANY, "fragments", " fragments:%u",
			   mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]));
	print_uint(PRINT_ANY, "fragmented", "/%u",
			   mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]));
	print_uint(PRINT_ANY, "bundles", " bundles:%u",
			   mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]));
	print_uint(PRINT_ANY, "bundled", "/%u\n",
			   mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
	close_json_object();

	open_json_object("tx packets");
	print_uint(PRINT_ANY, "tx packets", "  TX packets:%u",
			   mnl_attr_get_u32(attrs[TIPC_NLA_LINK_TX]) -
			   mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_INFO]));
	print_uint(PRINT_ANY, "fragments", " fragments:%u",
			   mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]));
	print_uint(PRINT_ANY, "fragmented", "/%u",
			   mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]));
	print_uint(PRINT_ANY, "bundles", " bundles:%u",
			   mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]));
	print_uint(PRINT_ANY, "bundled", "/%u\n",
			   mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
	close_json_object();

	proft = mnl_attr_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT]);
	print_uint(PRINT_ANY, "tx profile sample", "  TX profile sample:%u",
			   mnl_attr_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_CNT]));
	print_uint(PRINT_ANY, "packets average", " packets average:%u octets\n",
			   mnl_attr_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_TOT]) / proft);

	print_uint(PRINT_ANY, "0-64", "  0-64:%u%%",
			   perc(mnl_attr_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P0]), proft));
	print_uint(PRINT_ANY, "-256", " -256:%u%%",
			   perc(mnl_attr_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P1]), proft));
	print_uint(PRINT_ANY, "-1024", " -1024:%u%%",
			   perc(mnl_attr_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P2]), proft));
	print_uint(PRINT_ANY, "-4096", " -4096:%u%%",
			   perc(mnl_attr_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P3]), proft));
	print_uint(PRINT_ANY, "-16384", " -16384:%u%%",
			   perc(mnl_attr_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P4]), proft));
	print_uint(PRINT_ANY, "-32768", " -32768:%u%%",
			   perc(mnl_attr_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P5]), proft));
	print_uint(PRINT_ANY, "-66000", " -66000:%u%%\n",
			   perc(mnl_attr_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P6]), proft));

	open_json_object("rx states");
	print_uint(PRINT_ANY, "rx states", "  RX states:%u",
			   mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_STATES]));
	print_uint(PRINT_ANY, "probes", " probes:%u",
			   mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_PROBES]));
	print_uint(PRINT_ANY, "naks", " naks:%u",
			   mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]));
	print_uint(PRINT_ANY, "defs", " defs:%u",
			   mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]));
	print_uint(PRINT_ANY, "dups", " dups:%u\n",
			   mnl_attr_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
	close_json_object();

	open_json_object("tx states");
	print_uint(PRINT_ANY, "tx states", "  TX states:%u",
			   mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_STATES]));
	print_uint(PRINT_ANY, "probes", " probes:%u",
			   mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_PROBES]));
	print_uint(PRINT_ANY, "naks", " naks:%u",
			   mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]));
	print_uint(PRINT_ANY, "acks", " acks:%u",
			   mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]));
	print_uint(PRINT_ANY, "retrans", " retrans:%u\n",
			   mnl_attr_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
	close_json_object();

	print_uint(PRINT_ANY, "congestion link", "  Congestion link:%u",
			   mnl_attr_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]));
	print_uint(PRINT_ANY, "send queue max", "  Send queue max:%u",
			   mnl_attr_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]));
	print_uint(PRINT_ANY, "avg", " avg:%u\n",
			   mnl_attr_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));

	close_json_object();
	return MNL_CB_OK;
}