Исправлены все быги и все известные утечки памяти, исплавлена одновоточность, добавлен флаг --simulate-errors который добовляет ошибки в тестирование

This commit is contained in:
romenskiy 2025-06-27 16:57:43 +03:00
parent bf32765ea9
commit 733f7b528d
3 changed files with 185 additions and 152 deletions

View file

@ -7,6 +7,7 @@ void print_help(const char *progname) {
printf(" -i, --iterations N указать число повторения тестов\n");
printf(" -j, --json вывод в виде json\n");
printf(" -p, --pretest-delay N указать сколько секунд ждать\n\t\t\tперед проверкой памяти после записи\n");
printf(" --simulate-errors создаёт ошибки с вероятностью\n\t\t\t%%0.0001 каждый на 1 из 1000 проверок\n");
}
@ -18,6 +19,7 @@ void aaa(int* argc, char *argv[], int* pretest_delay){
{"iterations", required_argument, 0, 'i'},
{"json", no_argument, 0, 'j'},
{"pretest-delay", no_argument, 0, 'p'},
{"simulate-errors", no_argument, 0, 's'},
{0, 0, 0, 0}
};
@ -39,6 +41,9 @@ void aaa(int* argc, char *argv[], int* pretest_delay){
case 'j':
args.json = 1;
break;
case 's':
args.simulate_errors = 1;
break;
default:
print_help(argv[0]);
exit(1);

236
main.c
View file

@ -16,6 +16,7 @@ struct args_struct{
int iterations;
int json;
size_t words;
int simulate_errors;
};
struct args_struct args;
#include "cJSON/cJSON.h"
@ -51,7 +52,6 @@ struct test_info{
int number_addresses;
};
struct error_info{
uint32_t error_adres;
uint32_t expected;
uint32_t actual;
struct error_info* error_info_next;
@ -128,14 +128,14 @@ void meseng_test_and_slip(){
}
}
void test_patern(struct tred_pizdec** meta,unsigned long* thread, int tred, int* err, int p, int test_tipe){
void test_patern(struct tred_pizdec* meta,unsigned long* thread, int tred, int* err, int p, int test_tipe){
meseng_proses(test_tipe, p);
for (bool i = 0;(i != tred); i++){// test
meta[i]->id = p;
meta[i]->test_tip = test_tipe;
DEBUG_PRINT("words_stop: %zu\n",meta[i]->words_stop);
DEBUG_PRINT("words_start:%zu\n",meta[i]->words_start);
pthread_create(&thread[i], NULL, fill_pattern_multi_tred, (void*)meta[i]);
meta[i].id = p;
meta[i].test_tip = test_tipe;
DEBUG_PRINT("words_stop: %zu\n",meta[i].words_stop);
DEBUG_PRINT("words_start:%zu\n",meta[i].words_start);
pthread_create(&thread[i], NULL, fill_pattern_multi_tred, (void*)&meta[i]);
//pthread_join(thread[i], NULL);
}
for (bool i = 0;(i != tred); i++) //sink tred
@ -144,12 +144,12 @@ void test_patern(struct tred_pizdec** meta,unsigned long* thread, int tred, int*
meseng_test_and_slip();
//int err = check_pattern(mem, words, test_patterns[p]);
for (bool i = 0;(i != tred); i++){// test
pthread_create(&thread[i], NULL, check_pattern_multi_tred, (void*)meta[i]);
pthread_create(&thread[i], NULL, check_pattern_multi_tred, (void*)&meta[i]);
//pthread_join(thread[i], NULL);
}
for (bool i = 0;(i != tred); i++){ //sink tred
pthread_join(thread[i], NULL);
*err += meta[i]->return_error;
*err += meta[i].return_error;
}
if (!args.json){
if (*err == 0){
@ -160,67 +160,69 @@ void test_patern(struct tred_pizdec** meta,unsigned long* thread, int tred, int*
}
}
void json_create(struct tred_pizdec** meta){
void json_local_create(cJSON *error_info_array, struct error_info* error_info){
cJSON *error_local_info_array = cJSON_CreateObject();
{
char* char_dynamic = malloc(32);
sprintf(char_dynamic, "%p",(void*)&error_info->actual);
DEBUG_PRINT("%s\n", char_dynamic);
DEBUG_PRINT("0x%08X\n", error_info->actual);
cJSON *error_adres = cJSON_CreateString(char_dynamic);
cJSON_AddItemToObject(error_local_info_array, "error_adres", error_adres);
}{
char* char_dynamic = malloc(11);
sprintf(char_dynamic, "0x%08X",error_info->expected);
cJSON *expected = cJSON_CreateString(char_dynamic);
cJSON_AddItemToObject(error_local_info_array, "expected", expected);
}{
char* char_dynamic = malloc(11);
sprintf(char_dynamic, "0x%08X",error_info->actual);
cJSON *actual = cJSON_CreateString(char_dynamic);
cJSON_AddItemToObject(error_local_info_array, "actual", actual);
}
cJSON_AddItemToArray(error_info_array, error_local_info_array);
}
void json_sum_job(struct tred_pizdec* meta, cJSON *test_ob, char* char_dynamic, int i, unsigned int *error_int){
if (i < 4)
sprintf(char_dynamic, "0x%08X",test_patterns[i]);
else{
//sprintf(char_dynamic, "%s",test_name[i-4]);
//free(char_dynamic);
char_dynamic = test_name[i-4];
}
cJSON *error_ob = cJSON_CreateObject();
cJSON *error_info_array = cJSON_CreateArray();
unsigned int error_int_local=0;
for (int t=0; t != args.tred; t++){
struct error_info* error_info_A = meta[t].test_stats->test_local_stats[i]->error_info;
error_int_local += meta[t].test_stats->test_local_stats[i]->error_int;
while(NULL != error_info_A){
//for(){};
//p += meta[t].return_error;
json_local_create(error_info_array, error_info_A);
error_info_A = error_info_A->error_info_next;
}
}
*error_int += error_int_local;
cJSON *error_int_j = cJSON_CreateNumber(error_int_local);
cJSON_AddItemToObject(error_ob, "error", error_int_j);
cJSON_AddItemToObject(error_ob, "error_info", error_info_array);
cJSON_AddItemToObject(test_ob, char_dynamic, error_ob);
}
void json_create(struct tred_pizdec* meta){
cJSON *main_ob = cJSON_CreateObject();
//cJSON_AddItemToArray(main_Array, main_ob);
cJSON *test_ob = cJSON_CreateObject();
cJSON_AddItemToObject(main_ob, "test", test_ob);
char* putaa = malloc(11);
//char* char_dynamic = malloc(11);
char* char_dynamic = malloc(16);
unsigned int error_int=0;
for (int i = 0;i != sizeof(test_patterns)/sizeof(test_patterns[0]); ++i){
sprintf(putaa, "0x%08X",test_patterns[i]);
cJSON *error_ob = cJSON_CreateObject();
cJSON *error_info_array = cJSON_CreateArray();
unsigned int error_int_local=0;
for (int t=0; t != args.tred; t++){
struct error_info* error_info_A = meta[t]->test_stats->test_local_stats[i]->error_info;
error_int_local += meta[t]->test_stats->test_local_stats[i]->error_int;
while(NULL != error_info_A){
//for(){};
//p += meta[t]->return_error;
cJSON *error_local_info_array = cJSON_CreateObject();
{
char* putaa = malloc(16);
sprintf(putaa, "%p",(void*)&error_info_A->error_adres);
cJSON *error_adres = cJSON_CreateString(putaa);
cJSON_AddItemToObject(error_local_info_array, "error_adres", error_adres);
}
{
char* putaa = malloc(11);
sprintf(putaa, "0x%08X",error_info_A->expected);
cJSON *expected = cJSON_CreateString(putaa);
cJSON_AddItemToObject(error_local_info_array, "expected", expected);
}
{
char* putaa = malloc(11);
sprintf(putaa, "0x%08X",error_info_A->actual);
cJSON *actual = cJSON_CreateString(putaa);
cJSON_AddItemToObject(error_local_info_array, "actual", actual);
}
cJSON_AddItemToArray(error_info_array, error_local_info_array);
error_info_A = error_info_A->error_info_next;
}
}
error_int += error_int_local;
cJSON *error_int_j = cJSON_CreateNumber(error_int_local);
cJSON_AddItemToObject(error_ob, "error", error_int_j);
cJSON_AddItemToObject(error_ob, "error_info", error_info_array);
cJSON_AddItemToObject(test_ob, putaa, error_ob);
for (int i = 0;i != sizeof(test_patterns)/sizeof(test_patterns[0])+ sizeof(test_name)/sizeof(test_name[0]); ++i){
json_sum_job(meta, test_ob, char_dynamic, i, &error_int);
}
for (int i = 0;i != sizeof(test_name)/sizeof(test_name[0]); ++i){
int error_int_local=0;
cJSON *error_int_j = cJSON_CreateNumber(error_int_local);
cJSON *error_ob = cJSON_CreateObject();
cJSON *error_info_array = cJSON_CreateArray();
cJSON_AddItemToObject(error_ob, "error", error_int_j);
cJSON_AddItemToObject(error_ob, "error_info", error_info_array);
cJSON_AddItemToObject(test_ob, test_name[i], error_ob);
}
free(putaa);
free(char_dynamic);
cJSON *test_info_ob = cJSON_CreateObject();
cJSON_AddItemToObject(main_ob, "test_info", test_info_ob);
cJSON_AddItemToObject(main_ob, "test_infoAAA", test_info_ob);
cJSON *error_int_j = cJSON_CreateNumber(error_int);
cJSON_AddItemToObject(test_info_ob, "error", error_int_j);
cJSON *message_str;
@ -235,15 +237,29 @@ void json_create(struct tred_pizdec** meta){
cJSON_AddItemToObject(test_info_ob, "mod_test", mod_test_str);
char *string = cJSON_Print(main_ob);
printf("%s", string);
//cJSON *volume_js = cJSON_CreateNumber(volume);
//cJSON_AddItemToObject(main_ob, "volume", volume_js);
//cJSON_AddItemToObject(main_ob, "content", content_Array);
}
void mem_meta_create(struct tred_pizdec* meta, uint32_t *mem, int i){
//meta = malloc(sizeof(struct tred_pizdec));
meta->test_stats = malloc(sizeof(struct test_stats) +
(sizeof(test_list)/sizeof(test_list[0]) + sizeof(test_patterns)/sizeof(test_patterns[0])) * sizeof(struct test_local_stats*)
);
meta->test_stats->len = (sizeof(test_list)/sizeof(test_list[0]) + sizeof(test_patterns)/sizeof(test_patterns[0]));
//meta->test_stats = malloc(meta[i]->test_stats->len);
for (int m=0;m != meta->test_stats->len;m++){
meta->test_stats->test_local_stats[m] = malloc(sizeof(struct test_local_stats));
meta->test_stats->test_local_stats[m]->error_int = 0;
meta->test_stats->test_local_stats[m]->error_info = NULL;
//meta->test_stats->test_local_stats[m]->
}
meta->mem = mem;
meta->words_stop = args.words/args.tred*(i+1);
meta->words_start = i * args.words/args.tred;
meta->return_error = 0;
DEBUG_PRINT("words_stop: %zu\n",meta->words_stop);
DEBUG_PRINT("words_start:%zu\n",meta->words_start);
}
int main(int argc, char *argv[]){
if (mlockall(MCL_CURRENT | MCL_FUTURE) != 0) {
@ -251,9 +267,10 @@ int main(int argc, char *argv[]){
return 1;
}
args.tred = -1;
args.tred = 1;
args.iterations = 1;
args.json = 0;
args.simulate_errors = 0;
aaa(&argc, argv, &pretest_delay);
@ -268,67 +285,47 @@ int main(int argc, char *argv[]){
printf("Память не выделена!");
return 1;
}
if (args.tred < 1){
if (!args.json) printf("Количество потоков изменено на 1");
args.tred = 1;
}
//struct tred_pizdec* meta[];
//pthread_t thread[];
int err = 0, iterations = args.iterations;
struct tred_pizdec** meta = malloc(sizeof(struct tred_pizdec) * args.tred);
struct tred_pizdec* meta = malloc(sizeof(struct tred_pizdec) * args.tred);
pthread_t *thread = malloc(sizeof(pthread_t) * args.tred);
if (!args.json)
printf("iterations: %i\n", iterations);
if (args.tred > 1){
for (bool i = 0;(i != args.tred); i++)
mem_meta_create(&meta[i], mem, i);
DEBUG_PRINT("Выделена память!\n");
}
for (;iterations != 0; iterations--){
if (args.tred != -1){
for (bool i = 0;(i != args.tred); i++){// test
meta[i] = malloc(sizeof(struct tred_pizdec));
meta[i]->test_stats = malloc(sizeof(struct test_stats) +
(sizeof(test_list)/sizeof(test_list[0]) + sizeof(test_patterns)/sizeof(test_patterns[0])) * sizeof(struct test_local_stats*)
);
meta[i]->test_stats->len = (sizeof(test_list)/sizeof(test_list[0]) + sizeof(test_patterns)/sizeof(test_patterns[0]));
//meta[i]->test_stats = malloc(meta[i]->test_stats->len);
for (int m=0;m != meta[i]->test_stats->len;m++){
meta[i]->test_stats->test_local_stats[m] = malloc(sizeof(struct test_local_stats));
meta[i]->test_stats->test_local_stats[m]->error_int = 0;
meta[i]->test_stats->test_local_stats[m]->error_info = NULL;
//meta[i]->test_stats->test_local_stats[m]->
}
meta[i]->mem = mem;
meta[i]->words_stop = words/args.tred*(i+1);
meta[i]->words_start = i * words/args.tred;
meta[i]->return_error = 0;
DEBUG_PRINT("words_stop: %zu\n",meta[i]->words_stop);
DEBUG_PRINT("words_start:%zu\n",meta[i]->words_start);
}
if (args.tred > 1){
for (size_t p = 0; p < sizeof(test_patterns)/sizeof(test_patterns[0]); ++p){
test_patern(meta, thread, args.tred, &err, p, 1);// Основные паттерны 0x00000000, 0xFFFFFFFF, 0xAAAAAAAA, 0x55555555
}
for (int test_list_id = 0; test_list_id < sizeof(test_list)/sizeof(test_list[0]); ++test_list_id){
test_patern(meta, thread, args.tred, &err, 0, test_list[test_list_id]);
}
free(mem);
if (args.json)
json_create(meta);
for (bool i = 0;(i != args.tred); i++)
free(meta[i]);
}
else{
struct tred_pizdec meta;
meta.mem = mem;
meta.words_stop = words;
meta.words_start = 0;
meta.test_tip = 1;
mem_meta_create(meta, mem, 0);
meta->test_tip = 1;
// Основные паттерны 0x00000000, 0xFFFFFFFF, 0xAAAAAAAA, 0x55555555
for (size_t p = 0; p < sizeof(test_patterns)/sizeof(test_patterns[0]); ++p){
//struct tred_pizdec meta = malloc(sizeof(struct tred_pizdec));
meta.id = p;
meseng_proses(meta.test_tip, meta.id);
fill_pattern_multi_tred((void*)&meta);
meta->id = p;
meseng_proses(meta->test_tip, meta->id);
fill_pattern_multi_tred((void*)meta);
//fill_pattern(mem, words, test_patterns[p]);
meseng_test_and_slip();
check_pattern_multi_tred((void*)&meta);
err = meta.return_error;
check_pattern_multi_tred((void*)meta);
err += meta->return_error;
//err += check_pattern(mem, words, test_patterns[p]);
free(mem);
if (!args.json){
if (err == 0){
if (!err){
printf("\rOK\n");
fflush(stdout);
}
@ -336,20 +333,21 @@ int main(int argc, char *argv[]){
}
}
for (int test_list_id = 0; test_list_id < sizeof(test_list)/sizeof(test_list[0]); ++test_list_id){
meta.test_tip = test_list[test_list_id];
meseng_proses(meta.test_tip, meta.id);
fill_pattern_multi_tred((void*)&meta);
meta->test_tip = test_list[test_list_id];
meseng_proses(meta->test_tip, meta->id);
fill_pattern_multi_tred((void*)meta);
meseng_test_and_slip();
check_pattern_multi_tred((void*)&meta);
check_pattern_multi_tred((void*)meta);
if (!args.json){
if(meta.return_error == 0){
if(!meta->return_error){
printf("\rOK\n");
fflush(stdout);
}
else printf("Errors: %d\n", meta.return_error);
else printf("Errors: %d\n", meta->return_error);
}
err += meta->return_error;
if (!args.json){
if (err == 0){
if (!err){
printf("\rOK");
fflush(stdout);
}
@ -358,6 +356,10 @@ int main(int argc, char *argv[]){
}
}
}
free(mem);
if (args.json)
json_create(meta);
free(meta);
//if(args.json)
//for (bool i = 0;(args.tred != -1 || i != args.tred); i++)
// free(meta[i]);

View file

@ -9,72 +9,92 @@ int one_percent_chance(){
}
return (rand() % 1000000) == 0;
}
int a =1;
void error_simulet(size_t i, struct tred_pizdec* meta){
if (args.simulate_errors && i % 1000 == 0 && one_percent_chance()) {
meta->mem[i] = 45345345;
}
}
void* fill_pattern_multi_tred(void *arg) {
struct tred_pizdec* meta = (struct tred_pizdec*) arg;
DEBUG_PRINT("id: 0x%08X\n", test_patterns[meta->id]);
switch (meta->test_tip){
case 1:
for (size_t i = meta->words_start; i < meta->words_stop; i++){
if (a && i % 1000 == 0 && one_percent_chance()) {
meta->mem[i] = 45345345;
}
else
meta->mem[i] = test_patterns[meta->id];
error_simulet(i, meta);
}
break;
case 2:
for (size_t i = meta->words_start; i < meta->words_stop; i++)
for (size_t i = meta->words_start; i < meta->words_stop; i++){
meta->mem[i] = (uint32_t)i;
error_simulet(i, meta);
}
break;
case 3:
for (size_t i = meta->words_start; i < meta->words_stop; i++)
for (size_t i = meta->words_start; i < meta->words_stop; i++){
meta->mem[i] = 1U << (i % 32);
error_simulet(i, meta);
}
break;
case 4:
for (size_t i = meta->words_start; i < meta->words_stop; i++)
for (size_t i = meta->words_start; i < meta->words_stop; i++){
meta->mem[i] = ~(1U << (i % 32));
error_simulet(i, meta);
}
break;
}
return NULL;
}
void error_write_info(size_t i, struct tred_pizdec* meta, uint32_t expected){
int id = meta->id;
if (meta->test_tip != 1){
id = 2 + meta->test_tip;
}
if (args.json){
struct error_info* error_str = malloc(sizeof(struct error_info));
error_str->expected = expected;
error_str->actual = meta->mem[i];
if (!meta->test_stats->test_local_stats[id]->error_int){
if (meta->test_stats->test_local_stats[id] != NULL){
}
meta->test_stats->test_local_stats[id]->error_info = error_str;
}
else
meta->test_stats->test_local_stats[id]->error_info_end->error_info_next = error_str;
meta->test_stats->test_local_stats[id]->error_info_end = error_str;
}
else{
if (meta->test_tip == 1)
printf("\nОшибка в тесте 0x%08X: должен быть: 0x%08X, фактический: 0x%08X\n", test_patterns[id], expected, meta->mem[i]);
else
printf("\nОшибка в тесте %s: должен быть: 0x%08X, фактический: 0x%08X\n", test_name[meta->test_tip-2], expected, meta->mem[i]);
}
meta->test_stats->test_local_stats[id]->error_int++;
}
void* check_pattern_multi_tred(void *arg) {
struct tred_pizdec* meta = (struct tred_pizdec*) arg;
meta->return_error = 0;
//meta->return_error = 0;
switch (meta->test_tip){
case 1:
for (size_t i = meta->words_start; i < meta->words_stop; i++){
if (meta->mem[i] != test_patterns[meta->id]) {
if (args.json){
struct error_info* error_str = malloc(sizeof(struct error_info));
error_str->expected = test_patterns[meta->id];
error_str->actual = meta->mem[i];
error_str->error_adres = meta->mem[i];
if (!meta->test_stats->test_local_stats[meta->id]->error_int){
if (meta->test_stats->test_local_stats[meta->id] != NULL){
}
meta->test_stats->test_local_stats[meta->id]->error_info = error_str;
}
else
meta->test_stats->test_local_stats[meta->id]->error_info_end->error_info_next = error_str;
meta->test_stats->test_local_stats[meta->id]->error_info_end = error_str;
}
else{
printf("\nОшибка в тесте %zu: должен быть: 0x%08X, фактический: 0x%08X\n", i, test_patterns[meta->id], meta->mem[i]);
}
meta->test_stats->test_local_stats[meta->id]->error_int++;
if (meta->return_error > 10) break;
if (meta->mem[i] != test_patterns[meta->id]){
error_write_info(i, meta, test_patterns[meta->id]);
meta->return_error++;
//if (meta->return_error > 10) break;
}
}
break;
case 2:
for (size_t i = meta->words_start; i < meta->words_stop; i++) {
if (meta->mem[i] != (uint32_t)i) {
printf("\nОшибка в тесте %zu: должен быть: 0x%08X, фактический: 0x%08X\n", i, (uint32_t)i, meta->mem[i]);
error_write_info(i, meta, (uint32_t)i);
meta->return_error++;
if (meta->return_error > 10) break;
//if (meta->return_error > 10) break;
}
//printf("Адрес памяти: %p\n", (void*)meta->mem[i]);
}
@ -82,16 +102,22 @@ void* check_pattern_multi_tred(void *arg) {
case 3:
for (size_t i = meta->words_start; i < meta->words_stop; i++) {
if (meta->mem[i] != 1U << (i % 32)){
printf("\nПиздец_3\n");
error_write_info(i, meta, 1U << (i % 32));
meta->return_error++;
//if (meta->return_error > 10) break;
}
}
break;
case 4:
for (size_t i = meta->words_start; i < meta->words_stop; i++) {
//printf("\rTEST: 0x%08X ", ~(1U << (i % 32)));
if (meta->mem[i] != ~(1U << (i % 32))){
printf("\nПиздец_4\n");
DEBUG_PRINT("\n\nTEST: %p \n", (void*)&meta->mem[i]);
DEBUG_PRINT("TEST: 0x%08X \n", meta->mem[i]);
DEBUG_PRINT("TEST: 0x%08X \n\n", ~(1U << (i % 32)));
error_write_info(i, meta, ~(1U << (i % 32)));
meta->return_error++;
//if (meta->return_error > 10) break;
}
}
break;