static int ci_bonding_get_hash_policy(char *bond_name, int *policy) { char* buffer; char* pbuf; char* filename; int len; buffer = kmalloc(SYSFS_READ_BLOCK_SIZE + 1, GFP_KERNEL); if ( buffer == NULL ) return -ENOMEM; len = strlen(bond_name) + SYSFS_BASE_STRLEN + SYSFS_HASHPOLICY_LEAF_STRLEN + 1; filename = kmalloc(len, GFP_KERNEL); if ( filename == NULL ) { kfree(buffer); return -ENOMEM; } sprintf(filename, SYSFS_BASE, bond_name, SYSFS_HASHPOLICY_LEAF); len = ci_bonding_read_sysfs_file(filename, buffer, SYSFS_READ_BLOCK_SIZE); kfree(filename); if ( len < 0 ) { kfree(buffer); return len; } /* TODO if len == SYSFS_READ_BLOCK_SIZE there might be more to get */ pbuf = line_to_string(buffer, len, '\n', &len); if ( len == 0 ) { kfree(buffer); return -EINVAL; } pbuf = strnchr(buffer, len, ' '); if ( pbuf == NULL ) { kfree(buffer); return -EINVAL; } len = sscanf(pbuf, " %d", policy); if ( len != 1 ) { kfree(buffer); return -EINVAL; } kfree(buffer); return 0; }
static int ci_bonding_get_ab_active_slave(char *bond_name, char *active, int active_len) { char* buffer; char* filename; int len; buffer = kmalloc(SYSFS_READ_BLOCK_SIZE + 1, GFP_KERNEL); if ( buffer == NULL ) return -ENOMEM; len = strlen(bond_name) + SYSFS_BASE_STRLEN + SYSFS_ACTIVESLAVE_LEAF_STRLEN + 1; filename = kmalloc(len, GFP_KERNEL); if ( filename == NULL ) { kfree(buffer); return -ENOMEM; } sprintf(filename, SYSFS_BASE, bond_name, SYSFS_ACTIVESLAVE_LEAF); len = ci_bonding_read_sysfs_file(filename, buffer, SYSFS_READ_BLOCK_SIZE); kfree(filename); if ( len < 0 ) { kfree(buffer); return len; } if ( len == 0 ) { kfree(buffer); return -ENOENT; } /* TODO if len == SYSFS_READ_BLOCK_SIZE there might be more to get */ line_to_string(buffer, len, '\n', &len); if ( len == 0 ) { kfree(buffer); return -EINVAL; } if (len < active_len) strcpy(active, buffer); else { kfree(buffer); return -EINVAL; } kfree(buffer); return len; }
static int ci_bonding_get_list(char *filename, ci_dllist *output_list, ci_bonding_list_entry_fn *entry_fn, void *arg) { char* buffer; char* scratch; char* pbuf; struct ci_bonding_sysfs_read state; int rc, read_len = 0, line_len, buffer_offset = 0, leftover_len = 0; buffer = kmalloc(SYSFS_READ_BLOCK_SIZE + 1, GFP_KERNEL); if ( buffer == NULL ) return -ENOMEM; rc = ci_bonding_start_sysfs_read(filename, &state); if( rc != 0 ) { kfree(buffer); return rc; } do { leftover_len = read_len - buffer_offset; if( leftover_len ) { scratch = kmalloc(SYSFS_READ_BLOCK_SIZE + 1, GFP_KERNEL); if ( scratch == NULL ) { rc = -ENOMEM; goto done; } /* save the leftover section from previous read at the start of buffer */ memcpy(scratch, buffer+buffer_offset, leftover_len); memcpy(buffer, scratch, leftover_len); kfree(scratch); } /* Read the next block, being careful not to overwrite the * leftover section */ rc = ci_bonding_do_sysfs_read(&state, buffer + leftover_len, SYSFS_READ_BLOCK_SIZE - leftover_len); if( rc < 0 ) { if( ci_dllist_not_empty(output_list) ) { /* Squash the error as we've got some output in the list already */ rc = 0; } goto done; } if( rc == 0 ) goto done; read_len = rc + leftover_len; buffer_offset = 0; pbuf = buffer; do { pbuf = line_to_string(buffer + buffer_offset, read_len - buffer_offset, '\n', &line_len); if (line_len) { entry_fn(output_list, buffer+buffer_offset, line_len, arg); buffer_offset += line_len; } } while (buffer_offset < read_len && pbuf != NULL && line_len != 0); } while (read_len == SYSFS_READ_BLOCK_SIZE && buffer_offset > 0); rc = 0; done: kfree(buffer); ci_bonding_finish_sysfs_read(&state); return rc; }
/** * @brief ui_ggs_play * * Search the best move. * * @param ui User Interface. * @param turn Edax's color. */ static void ui_ggs_play(UI *ui, int turn) { long long real_time = -time_clock(); int remaining_time = ui->ggs->board->clock[turn].ini_time; int extra_time = ui->ggs->board->clock[turn].ext_time; Play *play; Result *result; char move[4], line[32]; const char *(search_state_array[6]) = {"running", "interrupted", "stop pondering", "out of time", "stopped on user demand", "completed"}; char search_state[32]; if (ui->is_same_play) { play = ui->play; if (search_count_tasks(ui->play->search) < options.n_task) { printf("<use a single %d tasks search while a single game is played>\n", options.n_task); play_stop_pondering(ui->play); search_set_task_number(ui->play[0].search, options.n_task); play_stop_pondering(ui->play + 1); search_set_task_number(ui->play[1].search, 0); } } else { play = ui->play + turn; if (search_count_tasks(ui->play->search) == options.n_task && options.n_task > 1) { printf("<split single %d tasks search into two %d task searches>\n", options.n_task, options.n_task / 2); play_stop_pondering(ui->play); search_set_task_number(ui->play[0].search, options.n_task / 2); play_stop_pondering(ui->play + 1); search_set_task_number(ui->play[1].search, options.n_task / 2); search_share(ui->play[0].search, ui->play[1].search); ui_ggs_ponder(ui, turn ^ 1); // ponder opponent move } } // game over detection... if (play_is_game_over(play)) { ggs_client_send(ui->ggs, "tell .%s *** GAME OVER ***\n", ui->ggs->me); return ; } result = play->result; if (remaining_time > 60000) remaining_time -= 10000; // keep 10s. for safety. else if (remaining_time > 10000) remaining_time -= 2000; // keep 2s. for safety. if (remaining_time < 1000) remaining_time = 1000; // set time to at list 1ms play_adjust_time(play, remaining_time, extra_time); printf("<ggs: go thinking>\n"); play_go(play, false); real_time += time_clock(); move_to_string(result->move, play->player, move); ggs_client_send(ui->ggs, "tell /os play %s %s/%d/%.2f\n", ui->ggs->board->id, move, result->score, 0.001 * (real_time + 1)); if (result->book_move) { printf("[%s plays %s in game %s ; score = %d from book]\n", ui->ggs->me, move, ui->ggs->board->id, result->score); ggs_client_send(ui->ggs, "tell .%s -----------------------------------------" "\\%s plays %s in game %s" "\\score == %d from book\n", ui->ggs->me, ui->ggs->me, move, ui->ggs->board->id, result->score ); } else if (play->search->n_empties >= 15) { //avoid noisy display const char *bound; char s_nodes[16], s_speed[16]; if (result->bound[result->move].lower < result->score && result->score == result->bound[result->move].upper) bound = "<="; else if (result->bound[result->move].lower == result->score && result->score < result->bound[result->move].upper) bound = ">="; else bound = "=="; info("<%s plays %s in game %s ; score = %d at %d@%d%% ; %lld nodes in %.1fs (%.0f nodes/s.)>\n", ui->ggs->me, move, ui->ggs->board->id, result->score, result->depth, selectivity_table[result->selectivity].percent, result->n_nodes, 0.001 * real_time, (result->n_nodes / (0.001 * real_time + 0.001)) ); if (play->search->stop == STOP_TIMEOUT) { sprintf(search_state, "%s at %d@%d%%", search_state_array[play->search->stop], play->search->depth, selectivity_table[play->search->selectivity].percent); } else { sprintf(search_state, "%s", search_state_array[play->search->stop]); } ggs_client_send(ui->ggs, "tell .%s -----------------------------------------" "\\%s plays %s in game %s using %d thread%s" "\\score %s %+02d at %d@%d%% ; PV: %s ;" "\\nodes: %s ; time: search = %.1fs, move = %.1fs; speed: %s." "\\search %s\n", ui->ggs->me, ui->ggs->me, move, ui->ggs->board->id, search_count_tasks(play->search), search_count_tasks(play->search) > 1 ? "s ;" : " ;", bound, result->score, result->depth, selectivity_table[result->selectivity].percent, line_to_string(result->pv, 8, " ", line), format_scientific(result->n_nodes, "N", s_nodes), 0.001 * result->time, 0.001 * real_time, format_scientific(result->n_nodes / (0.001 * result->time+ 0.001), "N/s", s_speed), search_state ); } }