static unsigned long property_get_size(const char *key) {
    char property[PROPERTY_VALUE_MAX];
    property_get(key, property, "");

    char *cp;
    unsigned long value = strtoul(property, &cp, 10);

    switch(*cp) {
    case 'm':
    case 'M':
        value *= 1024;
    /* FALLTHRU */
    case 'k':
    case 'K':
        value *= 1024;
    /* FALLTHRU */
    case '\0':
        break;

    default:
        value = 0;
    }

    if (!valid_size(value)) {
        value = 0;
    }

    return value;
}
示例#2
0
文件: vcopy.c 项目: E-LLP/VICAR
/*
 *	cpy_vld.  Copy a variable's valid structure
 *
 *	return FAIL if out of dynamic memory
 *
 */
FUNCTION VOID cpy_vld
(
    FUNINT		type,		/* in: variable type		*/
    GENPTR		in,		/* in: valid structure		*/
    GENPTR		out		/* out: valid structure 	*/

 )
    {
    FAST struct R_VALID *vin, *vout;	/* use any type to get count	*/

    
    vin = (struct R_VALID *)in;
    vout = (struct R_VALID *)out;
    bytmov (in, out, valid_size (type, (*vin).count));
    (*vout).count = (*vin).count;
    if (type == V_STRING)
#ifdef POINTER_VALIDS
	{
	COUNT i;
	struct S_VALID *svalidIn = (struct S_VALID *)  in;
	struct S_VALID *svalidOut = (struct S_VALID *) out;
	for (i=0; i < (*svalidIn).count; i++)
	    (*svalidOut).slist[i].string = s_save ((*svalidIn).slist[i].string);
	}
#endif
    return;
    }
// set the total space allocated to "id"
int LogBuffer::setSize(log_id_t id, unsigned long size) {
    // Reasonable limits ...
    if (!valid_size(size)) {
        return -1;
    }
    pthread_mutex_lock(&mLogElementsLock);
    log_buffer_size(id) = size;
    pthread_mutex_unlock(&mLogElementsLock);
    return 0;
}
示例#4
0
static int encode(const char *infile) {
    int     ok;
    image_t     image;
    const char  *outfile;

    if(use_verbose) {
        printf("encoding %s.. ", infile);
    }

    if(get_image(infile, &image) < 0) {
        fprintf(stderr, "failed reading %s\n", infile);
        return -EINVAL;
    }

    if(valid_size(image.w) == 0 || valid_size(image.h) == 0) {
        fprintf(stderr, "image dimensions for %s are not valid, see manual\n", infile);
        destroy_image(&image);
        return -EINVAL;
    }

    outfile = figure_outfilename(infile, "kmg");

    if(outfile == NULL) {
        fprintf(stderr, "memory allocation failed for %s\n", infile);
        return -ENOMEM;
    }

    /* Convert the input image to a 16-bit image according to parameters */
    convert_to_16(&image);

    /* Save it */
    ok = save(outfile, &image);

    destroy_image(&image);

    printf("\n");
    return ok;
}
示例#5
0
文件: sdp.c 项目: ghent360/bluez
static void decode_data_elements(uint32_t position, uint8_t indent,
				const uint8_t *data, uint32_t size,
				void (*print_func) (uint32_t, uint8_t, uint8_t,
						const uint8_t *, uint32_t))

{
	uint32_t datalen, elemlen, extrabits;
	int i;

	if (!size)
		return;

	extrabits = get_bits(data, size);

	if (size < 1 + (extrabits / 8)) {
		print_text(COLOR_ERROR, "data element descriptor too short");
		packet_hexdump(data, size);
		return;
	}

	datalen = get_size(data, size);

	if (size < 1 + (extrabits / 8) + datalen) {
		print_text(COLOR_ERROR, "data element size too short");
		packet_hexdump(data, size);
		return;
	}

	elemlen = 1 + (extrabits / 8) + datalen;

	for (i = 0; type_table[i].str; i++) {
		uint8_t type = (data[0] & 0xf8) >> 3;

		if (type_table[i].value != type)
			continue;

		if (print_func) {
			print_func(position, indent, type,
					data + 1 + (extrabits / 8), datalen);
			break;
		}

		print_field("%*c%s (%d) with %u byte%s [%u extra bits] len %u",
					indent, ' ', type_table[i].str, type,
					datalen, datalen == 1 ? "" : "s",
					extrabits, elemlen);
		if (!valid_size(data[0] & 0x07, type_table[i].sizes)) {
			print_text(COLOR_ERROR, "invalid data element size");
			packet_hexdump(data + 1 + (extrabits / 8), datalen);
			break;
		}

		if (type_table[i].recurse)
			decode_data_elements(0, indent + 2,
					data + 1 + (extrabits / 8), datalen,
								print_func);
		else if (type_table[i].print)
			type_table[i].print(indent + 2,
					data + 1 + (extrabits / 8), datalen);
		break;
	}

	if (elemlen > size) {
		print_text(COLOR_ERROR, "invalid data element size");
		return;
	}

	data += elemlen;
	size -= elemlen;

	decode_data_elements(position + 1, indent, data, size, print_func);
}
示例#6
0
int main(int argc, char * argv[])
{
        int i;
        int read_write_error;
        int input_stat[2] = {0};
        int output_stat[2] = {0};

        int flags = 0;
        int count = -1;
        int skip = 0, seek = 0;
        int bs = -1, ibs = 4096, obs = 4096;
        int buffer_size = 0;
        int count_read, count_write;
        time_t time_start, time_finish, time_use;

        const char * input_path = "-";
        const char * output_path = "-";
        const char * input_device = NULL;
        const char * output_device = NULL;

        struct io_base *input_handle, *output_handle;
        char * buffer_read, * buffer_write, * buffer_alloc;

        for (i = 1; i < argc; i++) {
                char * argline = argv[i];
                char * optvalue = getoptvalue(argv[i]);

                ARG_INT(&ibs, 1, "ibs=");
                ARG_INT(&obs, 2, "obs=");
                ARG_INT(&bs, (1 | 2), "bs=");
                ARG_INT(&seek, 4, "seek=");
                ARG_INT(&skip, 8, "skip=");
                ARG_INT(&count, 16, "count=");
                ARG_STRING(&input_path, 32, "if=");
                ARG_STRING(&output_path, 64, "of=");
                ARG_STRING(&input_device, 128, "kin=");
                ARG_STRING(&output_device, 256, "kout=");

                fprintf(stderr, "unkown operand %s", argline);
                exit(-1);
        }

        if (bs != -1) {
                ibs = bs;
                obs = bs;
        }

        valid_size("invalid input block size", ibs);
        valid_size("invalid output block size", obs);

        input_handle = open_file(input_path, GENERIC_READ);
        valid_handle("invalid input handle", input_handle);

        output_handle = open_file(output_path, GENERIC_WRITE);
        valid_handle("invalid output handle", output_handle);

        buffer_size = (ibs < obs? obs: ibs) * 2;
        buffer_alloc = (char *)malloc(buffer_size);
        valid_buffer("alloc buffer fail", buffer_alloc);

        if (seek > 0) {
				off_t posnew = seek * (off_t)(obs);
				off_t poscur = io_lseek(output_handle, posnew, SEEK_CUR);
                valid_size("seek output file fail", posnew == poscur);
        }

        if (skip > 0) {
				off_t posnew = skip * (off_t)(ibs);
				off_t poscur = io_lseek(output_handle, posnew, SEEK_CUR);
                valid_size("skip input file fail", posnew == poscur);
        }

        read_write_error = 0;
        count_read = count_write = 0;
        buffer_read = buffer_write = buffer_alloc;

        time_start = time(NULL);
        while (read_write_error == 0) {
                size_t transfer = 0;

                while (buffer_read < buffer_alloc + obs) {
                        if (!io_read(input_handle, buffer_read, ibs, &transfer)) {
                                read_write_error = 2;
                                break;
                        }

                        if (transfer == 0) {
                                read_write_error = 1;
                                break;
                        }

                        buffer_read += transfer;
                        count_read += transfer;

                        input_stat[transfer == ibs]++;
                        if (input_stat[0] + input_stat[1] == count) {
                                read_write_error = 1;
                                break;
                        }
                }

                while (buffer_write + obs <= buffer_read) {
                        if (!io_write(output_handle, buffer_write, obs, &transfer)) {
                                read_write_error = 2;
                                break;
                        }

                        if (transfer == 0) {
                                read_write_error = 2;
                                break;
                        }

                        output_stat[transfer == obs]++;
                        buffer_write += transfer;
                        count_write += transfer;
                }

                memmove(buffer_alloc, buffer_write, count_read - count_write);
                buffer_read = buffer_alloc + (count_read - count_write);
                buffer_write = buffer_alloc;
        }

        while (read_write_error == 1 &&
                        count_write < count_read) {
                size_t transfer = (count_read - count_write);

                valid_size("internal error", transfer < obs);
                if (io_write(output_handle, buffer_write, transfer, &transfer)) {
                        output_stat[transfer == obs]++;
                        buffer_write += transfer;
                        count_write += transfer;
                        continue;
                }

                if (io_write(output_handle, buffer_write, obs, &transfer)) {
                        output_stat[transfer == obs]++;
                        buffer_write += transfer;
                        count_write += transfer;
                        continue;
                }
           
                read_write_error = 3;
                break;
        }
        time_finish = time(NULL);

        io_close(output_handle);
        io_close(input_handle);
        free(buffer_alloc);

        time_use = time_finish > time_start? time_finish - time_start: 1;
        fprintf(stderr, "%d+%d records in\n", input_stat[1], input_stat[0]);
        fprintf(stderr, "%d+%d records out\n", output_stat[1], output_stat[0]);
        fprintf(stderr, "%d bytes transferred in %ld secs (%ld bytes/sec)\n",
                        count_read, time_use, count_read / time_use);
        return 0;
}
示例#7
0
int main (int argc, char ** argv)
{
	std::string filename;
	std::ifstream trace_file;

	uint64_t memory_size = 8*1024*1024; // default is 8GB
	// Check if all arguments are given in command line
	if (argc != 3)
	{
		std::cout << "Please supply two arguements: <trace file> <physical memory size (B)>." << std::endl;
		return -1;
	}

	// Get the filename
	filename.assign(argv[1]);
	// Assign the memory size
	memory_size = std::stol(argv[2], nullptr, 10);

	// allocate array
	uint64_t array_size = memory_size / page_size;
	array_size--; // assume 1st level page table ALWAYS in memory
	MEME * in_memory = new MEME[array_size];
//	in_memory [array_size];
	uint64_t array_index = 0;

	//std::cout << argv[2] << " " << memory_size << " " << array_size << std::endl;


	// Open the file
	trace_file.open(filename);
	char operation;
	std::string virtual_address;
	std::string this_key;
	int byte_size;
	std::string file_line;
	std::vector<std::string> line_input;
	std::string token;

	while (std::getline(trace_file, file_line))
	{
		// tokenize string
		std::istringstream ss(file_line);
		while(std::getline(ss, token, ' '))
		{
			line_input.push_back(token);
			//std::cout << token << std::endl;
		}

		if (line_input.size() != 3)
		{
			line_input.clear();
			continue;
		}
		
		operation = line_input[0].at(0);
		if (operation != 'R' && operation != 'W')
		{
			line_input.clear();
			continue;
		}

		line_input[1] = line_input[1].substr(0, line_input[1].size() - 3);
		if (!valid_address(line_input[1]))
		{
			line_input.clear();
			continue;
		}
		virtual_address = line_input[1];
		this_key = line_input[1];//virtual_address;//get_VPN(virtual_address);

		if (!valid_size(line_input[2]))
		{
			line_input.clear();
			continue;
		}
		byte_size = std::stoi(line_input[2]);

		if (operation == 'R')
			total_bytes_read += byte_size;
		else
			total_bytes_write += byte_size;

		auto search = vpn_tracker.find(this_key);
		if (search != vpn_tracker.end())
		{
			// check if 2nd level not in memory
			if (!vpn_tracker[this_key].lvl_2_mem)
			{
				// find page to eject
				array_index = get_next_index(in_memory, array_size, array_index);
				eject_from_memory (in_memory[array_index].vpn, in_memory[array_index].level);

				// insert new page
				insert_into_memory (in_memory, array_index, this_key, 2);
			}
			else if (vpn_tracker[this_key].lvl_2_mem)
			{
				total_accessed++;
				vpn_tracker[this_key].lvl_2_clock = 1;
			}

			// check if 3rd level not in memory
			if (!vpn_tracker[this_key].lvl_3_mem)
			{
				// find page to eject
				array_index = get_next_index(in_memory, array_size, array_index);
				eject_from_memory (in_memory[array_index].vpn, in_memory[array_index].level);

				// insert new page
				insert_into_memory (in_memory, array_index, this_key, 3);
			}
			else if (vpn_tracker[this_key].lvl_3_mem)
			{
				total_accessed++;
				vpn_tracker[this_key].lvl_3_clock = 1;
			}

			// check if 4th level not in memory
			if (!vpn_tracker[this_key].lvl_4_mem)
			{
				// find page to eject
				array_index = get_next_index(in_memory, array_size, array_index);
				eject_from_memory (in_memory[array_index].vpn, in_memory[array_index].level);

				// insert new page
				insert_into_memory (in_memory, array_index, this_key, 4);
			}
			else if (vpn_tracker[this_key].lvl_4_mem)
			{
				total_accessed++;
				vpn_tracker[this_key].lvl_4_clock = 1;
			}

			vpn_tracker[this_key].num_accessed += 1;
		}
		else
		{
			PTE new_elem = {this_key, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0};
			vpn_tracker.insert(std::pair<std::string, PTE>(this_key, new_elem));
			for (int i = 2; i < 5; ++i)
			{
				array_index = get_next_index(in_memory, array_size, array_index);
				eject_from_memory (in_memory[array_index].vpn, in_memory[array_index].level);
				insert_into_memory (in_memory, array_index, this_key, i);
			}
		}
		// std::cout << "num tokens: " << line_input.size() << std::endl;
		// {
		// 	virtual_address 
		// }
		//std::cout << operation << " " << std::hex << virtual_address << " " << std::hex << get_VPN(virtual_address) << std::endl;

		line_input.clear();
	}

	std::string most_accessed_vpn = "";
	uint64_t num_access_vpn = 0;
	for (auto& x: vpn_tracker)
	{
		if (x.second.num_accessed > num_access_vpn)
		{
			num_access_vpn = x.second.num_accessed;
			most_accessed_vpn = x.first;
		}
	}

	long double page_fault_rate = (long double) total_faults / (long double) total_accessed;

	std::cout << "Number of pages accessed: " << vpn_tracker.size() << std::endl;
	//std::cout << "faults " << total_faults << " accessed " << total_accessed << std::endl;
	std::cout << "Page fault rate: " << page_fault_rate << std::endl;
	std::cout << "Most accessed VPN: " << most_accessed_vpn << std::endl;
	std::cout << "Number of bytes read: " << total_bytes_read << std::endl;
	std::cout << "Number of bytes written: " << total_bytes_write << std::endl;
	std::cout << "Memory footprint: " << (page_size * (1 + vpn_tracker.size())) << std::endl;

	delete [] in_memory;

	// uint64_t hex_value = 0;
	// std::cin >> std::hex >> hex_value;

	// std::cout << std::hex << get_VPN(hex_value) << std::endl;

	return 0;
}