void log_RECORD_pool_add(char *file_defined, char *function_name, int line_define, float local_time, float total_time) { int i; unsigned int hash_val; bucket_t *bucket; entry_t *dst_entry; log_RECORD *new_rec; char source[MAX_SOURCE_STR_LEN]; /* hash source string , get dest entry by hash key */ snprintf(source, MAX_SOURCE_STR_LEN, "%s:%d\t%s", file_defined, line_define, function_name); hash_val = fnv_32a_str(source, FNV1_32A_INIT); bucket = &s_hash[hash_val % nbuckets]; dst_entry = NULL; for (i = 0; i < bucket->entry_num; i++) { if (hash_val == bucket->entrys[i].hash_val && !strcmp(source, bucket->entrys[i].record->source)) { dst_entry = &bucket->entrys[i]; dst_entry->record->call_times++; break; } } if (!dst_entry) { new_rec = (log_RECORD *)file_buffer_get_next_chunk(); if (!new_rec) { DBG(printf(">> Mark, empty new rec\n")); file_buffer_reset(); new_rec = (log_RECORD *)file_buffer_get_next_chunk(); ASSERT(new_rec, "File Buffer size is too small"); _log_RECORD_pool_reset(); } if (bucket->entry_num < __h_max_entrys) dst_entry = &bucket->entrys[bucket->entry_num++]; else { dst_entry = &bucket->entrys[0]; for (i = 1; i < bucket->entry_num; i++) { if (bucket->entrys[i].record->call_times < dst_entry->record->call_times) dst_entry = &bucket->entrys[i]; } } dst_entry->record = new_rec; strcpy(dst_entry->record->source, source); dst_entry->record->local_time = local_time; dst_entry->record->total_time = total_time; dst_entry->record->call_times = 1; dst_entry->hash_val = hash_val; } }
static unsigned font_desc_hash(void* buf, size_t len) { ass_font_desc_t* desc = buf; unsigned hval; hval = fnv_32a_str(desc->family, FNV1_32A_INIT); hval = fnv_32a_buf(&desc->bold, sizeof(desc->bold), hval); hval = fnv_32a_buf(&desc->italic, sizeof(desc->italic), hval); return hval; }
bool net_lundman_zfs_zvol::start (IOService *provider) { bool res = super::start(provider); IOLog("ZFS: Loading module ... \n"); /* * Initialize /dev/zfs, this calls spa_init->dmu_init->arc_init-> etc */ zfs_ioctl_osx_init(); ///sysctl_register_oid(&sysctl__debug_maczfs); //sysctl_register_oid(&sysctl__debug_maczfs_stalk); zfs_vfsops_init(); /* * When is the best time to start the system_taskq? It is strictly * speaking not used by SPL, but by ZFS. ZFS should really start it? */ system_taskq_init(); /* * hostid is left as 0 on OSX, and left to be set if developers wish to * use it. If it is 0, we will hash the hardware.uuid into a 32 bit * value and set the hostid. */ if (!zone_get_hostid(NULL)) { uint32_t myhostid = 0; IORegistryEntry *ioregroot = IORegistryEntry::getRegistryRoot(); if(ioregroot) { //IOLog("ioregroot is '%s'\n", ioregroot->getName(gIOServicePlane)); IORegistryEntry *macmodel = ioregroot->getChildEntry(gIOServicePlane); if(macmodel) { //IOLog("macmodel is '%s'\n", macmodel->getName(gIOServicePlane)); OSObject *ioplatformuuidobj; //ioplatformuuidobj = ioregroot->getProperty("IOPlatformUUID", gIOServicePlane, kIORegistryIterateRecursively); ioplatformuuidobj = macmodel->getProperty(kIOPlatformUUIDKey); if(ioplatformuuidobj) { OSString *ioplatformuuidstr = OSDynamicCast(OSString, ioplatformuuidobj); //IOLog("IOPlatformUUID is '%s'\n", ioplatformuuidstr->getCStringNoCopy()); myhostid = fnv_32a_str(ioplatformuuidstr->getCStringNoCopy(), FNV1_32A_INIT); sysctlbyname("kern.hostid", NULL, NULL, &myhostid, sizeof(myhostid)); printf("ZFS: hostid set to %08x from UUID '%s'\n", myhostid, ioplatformuuidstr->getCStringNoCopy()); } } } } return res; }
// font cache static unsigned font_hash(void *buf, size_t len) { ASS_FontDesc *desc = buf; unsigned hval; hval = fnv_32a_str(desc->family, FNV1_32A_INIT); hval = fnv_32a_buf(&desc->bold, sizeof(desc->bold), hval); hval = fnv_32a_buf(&desc->italic, sizeof(desc->italic), hval); hval = fnv_32a_buf(&desc->vertical, sizeof(desc->vertical), hval); return hval; }
static PyObject * fnvhash2_fnv_32a_str(PyObject *self, PyObject *args) { Fnv32_t fnv_32_res; const char *need_to_hash; if(!PyArg_ParseTuple(args, "s", &need_to_hash)) return NULL; fnv_32_res = fnv_32a_str(need_to_hash, (Fnv32_t)0); return Py_BuildValue("I",fnv_32_res); }
unsigned int ketama_hashi( char* inString ) { #if defined(ENABLE_FNV_HASH) return fnv_32a_str( inString, FNV1_32_INIT ); #else unsigned char digest[16]; ketama_md5_digest( inString, digest ); return (unsigned int)(( digest[3] << 24 ) | ( digest[2] << 16 ) | ( digest[1] << 8 ) | digest[0] ); #endif }
/* * \brief Create a hashcode for the drawing * XXX: To avoid collisions a better hash algorithm might be useful. */ void ass_drawing_hash(ASS_Drawing* drawing) { drawing->hash = fnv_32a_str(drawing->text, FNV1_32A_INIT); }
/** \brief Adds a specified server to the continuum that has just been added to the server list * \param addr The address of the server that you want to add points for. * \param newmemory The amount of allocated memory from the new server to be added to the cluster * \param cont Pointer to the continuum which we will refresh. * \param key Shared memory key for storing the newly created continuum. * \param slist The address of the list of servers that your building the continuum from. * \param numservers Number of servers available * \param memory Amount of memory available accross all servers * \param fmodtime File modtime * \return 0 on failure, 1 on success. */ int add_server_to_continuum(char* addr, unsigned long newmemory, continuum* cont, key_t key, serverinfo* slist, int numservers, unsigned long memory, time_t fmodtime) { int maxpoints = POINTS_PER_SERVER * numservers; // maximum number of ring points (HASH_COUNT * POINTS_PER_HASH * numservers) int numoldpoints = cont->numpoints; // total number of points in the ring prior to adding the server // Continuum will hold one mcs for each point on the circle: mcs *ring = (mcs*) malloc( maxpoints * sizeof(mcs) ); int i, k, indx = -1, numpoints = 0; // determine the number of hashes to create float ratio = (float)newmemory / (float)memory; // ratio of hashes for this server to total hashes int numhashes = floorf( ratio * HASH_COUNT * (float)numservers ); // number of hashes for this server //given unevenly distributed memory we need to limit number of hashes int max_hashcount = POINTS_PER_SERVER / POINTS_PER_HASH; if (numhashes > max_hashcount) { numhashes = max_hashcount; } #ifdef DEBUG int percent = floorf( ratio * 100.0 ); // percent of total hashes linked to this sever syslog( LOG_INFO, "Ketama: Server no. %d: %s (mem: %lu = %u%% or %d of %d)\n", i, addr, newmemory, percent, numhashes * POINTS_PER_HASH, HASH_COUNT * numservers * POINTS_PER_HASH ); #endif #if defined(ENABLE_FNV_HASH) Fnv32_t hval = FNV1_32_INIT; #endif // create the points on the ring for the new server for( k = 0; k < numhashes; k++ ) { #if defined(ENABLE_FNV_HASH) hval = fnv_32a_str(addr, hval); ring[numpoints].point = hval; snprintf( ring[numpoints].ip, sizeof(ring[numpoints].ip), "%s", addr); numpoints++; if (numpoints > maxpoints) { snprintf( k_error, sizeof(k_error), "Ketama: add_server_to_continuum tried to exceed mcs array bounds.\n" ); syslog1( LOG_INFO, k_error ); free(ring); free(slist); return 0; } #else char ss[30]; unsigned char digest[16]; snprintf( ss, sizeof(ss), "%s-%d", addr, k ); ketama_md5_digest( ss, digest ); // Use successive 4-bytes from hash as numbers for the points on the circle: int h; for( h = 0; h < POINTS_PER_HASH; h++ ) { ring[numpoints].point = ( digest[3+h*4] << 24 ) | ( digest[2+h*4] << 16 ) | ( digest[1+h*4] << 8 ) | digest[h*4]; snprintf( ring[numpoints].ip, sizeof(ring[numpoints].ip), "%s", addr); numpoints++; if (numpoints > maxpoints) { snprintf( k_error, sizeof(k_error), "Ketama: add_server_to_continuum tried to exceed mcs array bounds.\n" ); syslog1( LOG_INFO, k_error ); free(ring); free(slist); return 0; } } #endif } // append the points on the current ring (will be sorted in the reconstruction phase) for( i = 0; i < numoldpoints; i++ ) { memcpy( &ring[numpoints], &cont->array[i], sizeof(mcs) ); numpoints++; if (numpoints > maxpoints) { snprintf( k_error, sizeof(k_error), "Ketama: add_server_to_continuum tried to exceed mcs array bounds.\n" ); syslog1( LOG_INFO, k_error ); free(ring); free(slist); return 0; } } // reconstruct the continuum return reconstruct_continuum(key, slist, numservers, ring, numpoints, memory, fmodtime); }
/** \brief Loads the continuum of servers (each server as many points on a circle). * \param key Shared memory key for storing the newly created continuum. * \param slist The address of the list of servers that your building the continuum from. * \param numservers Number of servers available * \param memory Amount of memory available accross all servers * \param fmodtime File modtime * \return 0 on failure, 1 on success. */ int load_continuum(key_t key, serverinfo* slist, int numservers, unsigned long memory, time_t fmodtime) { int maxpoints = POINTS_PER_SERVER * numservers; // maximum number of ring points (HASH_COUNT * POINTS_PER_HASH * numservers) // Continuum will hold one mcs for each point on the circle: mcs *ring = (mcs*) malloc( maxpoints * sizeof(mcs) ); int i, k, numpoints = 0; // buildup the continuum ring for( i = 0; i < numservers; i++ ) { float ratio = (float) slist[i].memory / (float)memory; // ratio of hashes for this server to total hashes int numhashes = floorf( ratio * HASH_COUNT * (float)numservers ); // number of hashes for this server #ifdef DEBUG int percent = floorf( ratio * 100.0 ); // percent of total hashes linked to this sever syslog( LOG_INFO, "Ketama: Server no. %d: %s (mem: %lu = %u%% or %d of %d)\n", i, slist[i].addr, slist[i].memory, percent, numhashes * POINTS_PER_HASH, HASH_COUNT * numservers * POINTS_PER_HASH ); #endif #if defined(ENABLE_FNV_HASH) Fnv32_t hval = FNV1_32_INIT; #endif // create the points on the ring for this server for( k = 0; k < numhashes; k++ ) { #if defined(ENABLE_FNV_HASH) hval = fnv_32a_str(slist[i].addr, hval); ring[numpoints].point = hval; snprintf( ring[numpoints].ip, sizeof(ring[numpoints].ip), "%s", slist[i].addr); numpoints++; #else char ss[30]; unsigned char digest[16]; snprintf( ss, sizeof(ss), "%s-%d", slist[i].addr, k ); ketama_md5_digest( ss, digest ); // Use successive 4-bytes from hash as numbers for the points on the circle: int h; for( h = 0; h < POINTS_PER_HASH; h++ ) { ring[numpoints].point = ( digest[3+h*4] << 24 ) | ( digest[2+h*4] << 16 ) | ( digest[1+h*4] << 8 ) | digest[h*4]; snprintf( ring[numpoints].ip, sizeof(ring[numpoints].ip), "%s", slist[i].addr); numpoints++; if (numpoints > maxpoints) { snprintf( k_error, sizeof(k_error), "Ketama: load_continuum tried to exceed mcs array bounds.\n" ); syslog1( LOG_INFO, k_error ); free(ring); free(slist); return 0; } } #endif } } return reconstruct_continuum(key, slist, numservers, ring, numpoints, memory, fmodtime); }
/* * main - the main function * * See the above usage for details. */ int main(int argc, char *argv[]) { char buf[BUF_SIZE+1]; /* read buffer */ int readcnt; /* number of characters written */ Fnv32_t hval; /* current hash value */ int s_flag = 0; /* 1 => -s was given, hash args as strings */ int m_flag = 0; /* 1 => print multiple hashes, one per arg */ int v_flag = 0; /* 1 => verbose hash print */ int b_flag = WIDTH; /* -b flag value */ int t_flag = -1; /* FNV test vector code (0=>print, 1=>test) */ enum fnv_type hash_type = FNV_NONE; /* type of FNV hash to perform */ Fnv32_t bmask; /* mask to apply to output */ extern char *optarg; /* option argument */ extern int optind; /* argv index of the next arg */ int fd; /* open file to process */ char *p; int i; /* * parse args */ program = argv[0]; while ((i = getopt(argc, argv, "b:mst:v")) != -1) { switch (i) { case 'b': /* bcnt bit mask count */ b_flag = atoi(optarg); break; case 'm': /* print multiple hashes, one per arg */ m_flag = 1; break; case 's': /* hash args as strings */ s_flag = 1; break; case 't': /* FNV test vector code */ t_flag = atoi(optarg); if (t_flag < 0 || t_flag > 1) { fprintf(stderr, "%s: -t code must be 0 or 1\n", program); fprintf(stderr, usage, program, FNV_VERSION); exit(1); } m_flag = 1; break; case 'v': /* verbose hash print */ m_flag = 1; v_flag = 1; break; default: fprintf(stderr, usage, program, FNV_VERSION); exit(1); } } /* -t code incompatible with -b, -m and args */ if (t_flag >= 0) { if (b_flag != WIDTH) { fprintf(stderr, "%s: -t code incompatible with -b\n", program); exit(2); } if (s_flag != 0) { fprintf(stderr, "%s: -t code incompatible with -s\n", program); exit(3); } if (optind < argc) { fprintf(stderr, "%s: -t code incompatible args\n", program); exit(4); } } /* -s requires at least 1 arg */ if (s_flag && optind >= argc) { fprintf(stderr, usage, program, FNV_VERSION); exit(5); } /* limit -b values */ if (b_flag < 0 || b_flag > WIDTH) { fprintf(stderr, "%s: -b bcnt: %d must be >= 0 and < %d\n", program, b_flag, WIDTH); exit(6); } if (b_flag == WIDTH) { bmask = (Fnv32_t)0xffffffff; } else { bmask = (Fnv32_t)((1 << b_flag) - 1); } /* * start with the initial basis depending on the hash type */ p = strrchr(program, '/'); if (p == NULL) { p = program; } else { ++p; } if (strcmp(p, "fnv032") == 0) { /* using non-recommended FNV-0 and zero initial basis */ hval = FNV0_32_INIT; hash_type = FNV0_32; } else if (strcmp(p, "fnv132") == 0) { /* using FNV-1 and non-zero initial basis */ hval = FNV1_32_INIT; hash_type = FNV1_32; } else if (strcmp(p, "fnv1a32") == 0) { /* start with the FNV-1a initial basis */ hval = FNV1_32A_INIT; hash_type = FNV1a_32; } else { fprintf(stderr, "%s: unknown program name, unknown hash type\n", program); exit(7); } /* * FNV test vector processing, if needed */ if (t_flag >= 0) { int code; /* test vector that failed, starting at 1 */ /* * perform all tests */ code = test_fnv32(hash_type, hval, bmask, v_flag, t_flag); /* * evaluate the tests */ if (code == 0) { if (v_flag) { printf("passed\n"); } exit(0); } else { printf("failed vector (1 is 1st test): %d\n", code); exit(8); } } /* * string hashing */ if (s_flag) { /* hash any other strings */ for (i=optind; i < argc; ++i) { switch (hash_type) { case FNV0_32: case FNV1_32: hval = fnv_32_str(argv[i], hval); break; case FNV1a_32: hval = fnv_32a_str(argv[i], hval); break; default: unknown_hash_type(program, hash_type, 9); /* exit(9) */ /*NOTREACHED*/ } if (m_flag) { print_fnv32(hval, bmask, v_flag, argv[i]); } } /* * file hashing */ } else { /* * case: process only stdin */ if (optind >= argc) { /* case: process only stdin */ while ((readcnt = read(0, buf, BUF_SIZE)) > 0) { switch (hash_type) { case FNV0_32: case FNV1_32: hval = fnv_32_buf(buf, readcnt, hval); break; case FNV1a_32: hval = fnv_32a_buf(buf, readcnt, hval); default: unknown_hash_type(program, hash_type, 10); /* exit(10) */ /*NOTREACHED*/ } } if (m_flag) { print_fnv32(hval, bmask, v_flag, "(stdin)"); } } else { /* * process any other files */ for (i=optind; i < argc; ++i) { /* open the file */ fd = open(argv[i], O_RDONLY); if (fd < 0) { fprintf(stderr, "%s: unable to open file: %s\n", program, argv[i]); exit(4); } /* hash the file */ while ((readcnt = read(fd, buf, BUF_SIZE)) > 0) { switch (hash_type) { case FNV0_32: case FNV1_32: hval = fnv_32_buf(buf, readcnt, hval); break; case FNV1a_32: hval = fnv_32a_buf(buf, readcnt, hval); default: unknown_hash_type(program, hash_type, 11);/* exit(11) */ /*NOTREACHED*/ } } /* finish processing the file */ if (m_flag) { print_fnv32(hval, bmask, v_flag, argv[i]); } close(fd); } } } /* * report hash and exit */ if (!m_flag) { print_fnv32(hval, bmask, v_flag, ""); } return 0; /* exit(0); */ }