dnp_sim2.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright 2001 Sun Microsystems, Inc.
  3. * Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
  4. * All rights reserved.
  5. * END COPYRIGHT BLOCK **/
  6. /* dnp_simulation.c - this file varifies the correctness of dnp algorithm
  7. by generating random sequences of operations, applying
  8. the algorithm and outputing the result
  9. usage: dnp_sim [-h] [-n <number of simulations> ] [-v] [-f <output file>]
  10. -h - print usage information.
  11. -n <number of simulations> - how many simulations to perform; default - 1.
  12. -v - verbose mode (prints full entry state after each operation execution)
  13. -f <output file> - file where results are stored; by default results are
  14. printed to the screen.
  15. -o <op file> - file that contains operation sequence to execute; by default,
  16. random sequence is generated.
  17. */
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <errno.h>
  21. #include <memory.h>
  22. #include <string.h>
  23. #include <time.h>
  24. #include <windows.h>
  25. #define MAX_OPS 18 /* maximum number of operations in a simulation */
  26. #define MAX_VALS 10 /* maximum number of values is entry or dn */
  27. #define NOT_PRESENT -1
  28. /* data types */
  29. typedef struct value_state
  30. {
  31. int value_index; /* value */
  32. int presence_csn; /* last time at which we know the value was present */
  33. int distinguished_index; /* index into dncsn list */
  34. int delete_csn; /* last attempt to delete this value */
  35. int present; /* flag that tells whether the value iscurrently present */
  36. } Value_State;
  37. typedef struct dn_csn
  38. {
  39. int csn; /* dn csn */
  40. int value_index; /* dn value */
  41. } Dn_Csn;
  42. typedef struct entry_state
  43. {
  44. Dn_Csn dn_csns [MAX_VALS + 1]; /* list of dn csns for this entry */
  45. int dn_csn_count; /* csn of the current dn */
  46. Value_State values[MAX_VALS]; /* values of the attribute */
  47. int attr_delete_csn; /* last attempt to delete the entire attribute */
  48. } Entry_State;
  49. typedef enum
  50. {
  51. OP_ADD_VALUE,
  52. OP_DELETE_VALUE,
  53. OP_RENAME_ENTRY,
  54. OP_DELETE_ATTR,
  55. OP_END
  56. } Operation_Type;
  57. typedef struct operation
  58. {
  59. Operation_Type type; /* operation type */
  60. int csn; /* operation type */
  61. int value_index; /* value to add, remove or rename from */
  62. }Operation;
  63. typedef struct simulator_params
  64. {
  65. int runs; /* number of runs */
  66. FILE *fout; /* output file */
  67. int value_count; /* number of values */
  68. int verbose; /* verbose mode */
  69. Operation *ops; /* operation sequence to apply */
  70. int op_count;
  71. }Simulator_Params;
  72. /* gloabl data */
  73. Simulator_Params sim;
  74. char *g_values[] =
  75. {
  76. "v",
  77. "u",
  78. "w",
  79. NULL
  80. };
  81. /* forward declarations */
  82. /* initialization */
  83. void process_cmd (int argc, char **argv);
  84. void set_default_sim_params ();
  85. int count_values ();
  86. void parse_operations_file (char *name);
  87. void parse_operation (char *line, int pos);
  88. int value2index (char *value);
  89. void print_usage ();
  90. /* simulation run */
  91. void run_simulation ();
  92. void generate_operations (Operation **ops, int *op_count);
  93. void generate_operation (Operation *op, int csn);
  94. int* generate_operation_order (int op_count, int seq_num);
  95. void apply_operation_sequence (Operation *ops, int op_count, int *order, Entry_State *entry);
  96. void init_entry_state (Entry_State *entry);
  97. void init_value_state (Value_State *val, int seq_num);
  98. void apply_operation (Entry_State *entry, Operation *op);
  99. void free_operations (Operation **ops);
  100. int ** new_perm_table (int op_count);
  101. void free_perm_table (int ***perm_table, int op_count);
  102. int get_perm_count (int op_count);
  103. void generate_perm_table (int *elements, int element_count, int static_part,
  104. int **perm_table);
  105. void apply_add_operation (Entry_State *entry, Operation *op);
  106. void apply_value_delete_operation (Entry_State *entry, Operation *op);
  107. void apply_attr_delete_operation (Entry_State *entry, Operation *op);
  108. void apply_rename_operation (Entry_State *entry, Operation *op);
  109. void purge_value_state (Entry_State *entry, int index);
  110. void resolve_value_state (Entry_State *entry, int value_index);
  111. int value_distinguished_at_delete (Entry_State *entry, int value_index);
  112. int compare_entry_state (Entry_State *entry1, Entry_State *entry2, int run);
  113. /* dnc_csn handling */
  114. int dn_csn_add (Entry_State *entry, int value_index, int csn);
  115. int get_value_dn_csn (Entry_State *entry, int value_index);
  116. /* data tracing */
  117. void dump_operations (Operation *ops, int op_count, int *order);
  118. void dump_operation (Operation *op);
  119. void dump_perm_table (int **perm_table, int op_count);
  120. void dump_entry_state (Entry_State *entry);
  121. void dump_value_state (Value_State *value);
  122. void dump_dn_csn_list (Entry_State *entry);
  123. /* misc functions */
  124. int max_val (int a, int b);
  125. int main (int argc, char **argv)
  126. {
  127. int i;
  128. process_cmd (argc, argv);
  129. for (i = 0; i < sim.runs; i++)
  130. {
  131. fprintf (sim.fout, "*******running simulation #%d ...\n\n", i+1);
  132. run_simulation ();
  133. fprintf (sim.fout, "\n*******done with simulation #%d ...\n\n", i+1);
  134. }
  135. if (sim.fout != stdout)
  136. fclose (sim.fout);
  137. return 0;
  138. }
  139. void process_cmd (int argc, char **argv)
  140. {
  141. int i;
  142. set_default_sim_params ();
  143. if (argc == 1)
  144. {
  145. return;
  146. }
  147. if (strcmp (argv[1], "-h") == 0) /* print help */
  148. {
  149. print_usage ();
  150. exit (0);
  151. }
  152. i = 1;
  153. while (i < argc)
  154. {
  155. if (strcmp (argv[i], "-v") == 0) /* verbose mode */
  156. {
  157. sim.verbose = 1;
  158. i ++;
  159. }
  160. else if (strcmp (argv[i], "-n") == 0)
  161. {
  162. if (i < argc - 1)
  163. {
  164. int runs = atoi (argv[i + 1]);
  165. if (runs > 0)
  166. sim.runs = runs;
  167. i+=2;
  168. }
  169. else
  170. {
  171. /* ONREPL print warning */
  172. i++;
  173. }
  174. }
  175. else if (strcmp (argv[i], "-f") == 0) /* output file */
  176. {
  177. if (i < argc - 1)
  178. {
  179. FILE *f = fopen (argv[i + 1], "w");
  180. if (f == 0)
  181. {
  182. printf ("failed to open output file; error - %s, using stdout\n",
  183. strerror(errno));
  184. }
  185. else
  186. {
  187. /* ONREPL print warning */
  188. sim.fout = f;
  189. }
  190. i += 2;
  191. }
  192. else
  193. i++;
  194. }
  195. else if (strcmp (argv[i], "-o") == 0) /* file with operation sequence */
  196. {
  197. if (i < argc - 1)
  198. {
  199. parse_operations_file (argv[i+1]);
  200. i += 2;
  201. }
  202. else
  203. {
  204. /* ONREPL print warning */
  205. i ++;
  206. }
  207. }
  208. else /* unknown option */
  209. {
  210. printf ("unknown option - %s; ignored\n", argv[i]);
  211. i ++;
  212. }
  213. }
  214. }
  215. void set_default_sim_params ()
  216. {
  217. memset (&sim, 0, sizeof (sim));
  218. sim.runs = 1;
  219. sim.fout = stdout;
  220. sim.value_count = count_values ();
  221. }
  222. /* file format: <op count>
  223. add <value>
  224. delete <value>
  225. delete attribute
  226. rename to <value>
  227. all spaces are significant
  228. */
  229. void parse_operations_file (char *name)
  230. {
  231. FILE *file = fopen (name, "r");
  232. char line [256];
  233. int i;
  234. if (file == NULL)
  235. {
  236. printf ("failed to open operations file %s: error = %d\n", name, errno);
  237. print_usage ();
  238. exit (1);
  239. }
  240. i = 0;
  241. while (fgets (line, sizeof (line), file))
  242. {
  243. if (i == 0)
  244. {
  245. /* read operation count */
  246. sim.op_count = atoi (line);
  247. if (sim.op_count < 1 || sim.op_count > MAX_OPS/2)
  248. {
  249. printf ("invalid operation count - %d; value must be between 1 and %d\n",
  250. sim.op_count, MAX_OPS/2);
  251. print_usage ();
  252. exit (1);
  253. }
  254. else
  255. {
  256. sim.ops = (Operation*)malloc (sim.op_count * sizeof (Operation));
  257. }
  258. }
  259. else
  260. {
  261. if (strlen (line) == 0) /* skip empty lines */
  262. continue;
  263. parse_operation (line, i);
  264. }
  265. i ++;
  266. }
  267. }
  268. void parse_operation (char *line, int i)
  269. {
  270. sim.ops [i - 1].csn = i;
  271. if (line[strlen(line) - 1] == '\n')
  272. line[strlen(line) - 1] = '\0';
  273. if (strncmp (line, "add", 3) == 0)
  274. {
  275. sim.ops [i - 1].type = OP_ADD_VALUE;
  276. sim.ops [i - 1].value_index = value2index (&line[4]);
  277. }
  278. else if (strncmp (line, "delete attribute", 16) == 0)
  279. {
  280. sim.ops [i - 1].type = OP_DELETE_ATTR;
  281. }
  282. else if (strncmp (line, "delete", 6) == 0)
  283. {
  284. sim.ops [i - 1].type = OP_DELETE_VALUE;
  285. sim.ops [i - 1].value_index = value2index (&line[7]);
  286. }
  287. else if (strncmp (line, "rename to", 6) == 0)
  288. {
  289. sim.ops [i - 1].type = OP_RENAME_ENTRY;
  290. sim.ops [i - 1].value_index = value2index (&line[10]);
  291. }
  292. else
  293. {
  294. /* invalid line */
  295. printf ("invalid operation: %s\n", line);
  296. exit (1);
  297. }
  298. }
  299. int value2index (char *value)
  300. {
  301. int i;
  302. for (i = 0; i < sim.value_count; i++)
  303. {
  304. if (strcmp (g_values[i], value) == 0)
  305. return i;
  306. }
  307. return -1;
  308. }
  309. void print_usage ()
  310. {
  311. printf ("usage: dnp_sim [-h] [-n <number of simulations> ] [-v] [-f <output file>]\n"
  312. "\t-h - print usage information\n"
  313. "\t-n <number of simulations>; default - 1\n"
  314. "\t-v - verbose mode\n"
  315. "\t-f <output file> - by default, results are printed to the screen\n"
  316. "\t-o <op file> - file that contains operation sequence to execute;\n"
  317. "\tby default, random sequence is generated.\n");
  318. }
  319. int count_values ()
  320. {
  321. int i;
  322. for (i = 0; g_values[i]; i++);
  323. return i;
  324. }
  325. void run_simulation ()
  326. {
  327. int *order;
  328. int i;
  329. int perm_count;
  330. Entry_State entry_first, entry_current;
  331. int error = 0;
  332. init_entry_state (&entry_first);
  333. fprintf (sim.fout, "initial entry state :\n");
  334. dump_entry_state (&entry_first);
  335. if (sim.ops == NULL)
  336. {
  337. generate_operations (&sim.ops, &sim.op_count);
  338. }
  339. fprintf (sim.fout, "initial operation set:\n");
  340. dump_operations (sim.ops, sim.op_count, NULL/* order */);
  341. //DebugBreak ();
  342. perm_count = get_perm_count (sim.op_count);
  343. for (i = 0; i < perm_count; i++)
  344. {
  345. fprintf (sim.fout, "--------------------------------\n");
  346. fprintf (sim.fout, "simulation run %d\n", i + 1);
  347. fprintf (sim.fout, "--------------------------------\n");
  348. order = generate_operation_order (sim.op_count, i);
  349. if (i == 0)
  350. apply_operation_sequence (sim.ops, sim.op_count, order, &entry_first);
  351. else
  352. {
  353. apply_operation_sequence (sim.ops, sim.op_count, order, &entry_current);
  354. error |= compare_entry_state (&entry_first, &entry_current, i + 1);
  355. }
  356. }
  357. switch (error)
  358. {
  359. case 0: fprintf (sim.fout, "all runs left the entry in the same state\n");
  360. break;
  361. case 1: fprintf (sim.fout, "while value presence is consistent across all runs, "
  362. "the exact state does not match\n");
  363. break;
  364. case 3: fprintf (sim.fout, "the runs left entries in an inconsistent state\n");
  365. break;
  366. }
  367. free_operations (&sim.ops);
  368. }
  369. void generate_operations (Operation **ops, int *op_count)
  370. {
  371. int i;
  372. /* generate number operations in the sequence */
  373. *op_count = slapi_rand () % (MAX_OPS / 2) + 1;
  374. *ops = (Operation *)malloc (*op_count * sizeof (Operation));
  375. for (i = 0; i < *op_count; i ++)
  376. {
  377. generate_operation (&((*ops)[i]), i + 1);
  378. }
  379. }
  380. void generate_operation (Operation *op, int csn)
  381. {
  382. /* generate operation type */
  383. op->type = slapi_rand () % OP_END;
  384. /* generate value to which operation applies */
  385. op->value_index = slapi_rand () % sim.value_count;
  386. op->csn = csn;
  387. }
  388. int* generate_operation_order (int op_count, int seq_num)
  389. {
  390. static int **perm_table = NULL;
  391. /* first request - generate pemutation table */
  392. if (seq_num == 0)
  393. {
  394. int elements [MAX_OPS];
  395. int i;
  396. if (perm_table)
  397. free_perm_table (&perm_table, op_count);
  398. perm_table = new_perm_table (op_count);
  399. for (i = 0; i < op_count; i++)
  400. elements [i] = i;
  401. generate_perm_table (elements, op_count, 0 /* static part */,
  402. perm_table);
  403. }
  404. return perm_table [seq_num];
  405. }
  406. void apply_operation_sequence (Operation *ops, int op_count, int *order, Entry_State *entry)
  407. {
  408. int i;
  409. init_entry_state (entry);
  410. if (!sim.verbose)
  411. {
  412. if (!sim.verbose)
  413. {
  414. fprintf (sim.fout, "operation_sequence for this run:\n");
  415. dump_operations (ops, op_count, order);
  416. }
  417. }
  418. for (i = 0; i < op_count; i++)
  419. {
  420. apply_operation (entry, &(ops [order[i]]));
  421. }
  422. if (!sim.verbose)
  423. {
  424. fprintf (sim.fout, "final entry state :\n");
  425. dump_entry_state (entry);
  426. }
  427. }
  428. void init_entry_state (Entry_State *entry)
  429. {
  430. int i;
  431. memset (entry, 0, sizeof (*entry));
  432. entry->attr_delete_csn = NOT_PRESENT;
  433. entry->dn_csn_count = 1;
  434. for (i = 0; i < sim.value_count; i++)
  435. {
  436. init_value_state (&(entry->values[i]), i);
  437. }
  438. }
  439. void init_value_state (Value_State *val, int seq_num)
  440. {
  441. memset (val, 0, sizeof (*val));
  442. val->value_index = seq_num;
  443. val->present = 1;
  444. val->delete_csn = NOT_PRESENT;
  445. if (seq_num > 0) /* only first value is distinguished */
  446. val->distinguished_index = -1;
  447. }
  448. void apply_operation (Entry_State *entry, Operation *op)
  449. {
  450. switch (op->type)
  451. {
  452. case OP_ADD_VALUE: apply_add_operation (entry, op);
  453. break;
  454. case OP_DELETE_VALUE: apply_value_delete_operation (entry, op);
  455. break;
  456. case OP_DELETE_ATTR: apply_attr_delete_operation (entry, op);
  457. break;
  458. case OP_RENAME_ENTRY: apply_rename_operation (entry, op);
  459. break;
  460. }
  461. if (sim.verbose)
  462. {
  463. fprintf (sim.fout, "operation: ");
  464. dump_operation (op);
  465. fprintf (sim.fout, "\n");
  466. dump_entry_state (entry);
  467. }
  468. }
  469. void free_operations (Operation **ops)
  470. {
  471. free (*ops);
  472. *ops = NULL;
  473. }
  474. int **new_perm_table (int op_count)
  475. {
  476. int i;
  477. int **perm_table;
  478. int perm_count = get_perm_count (op_count);
  479. perm_table = (int**)malloc (perm_count * sizeof (int*));
  480. for (i = 0; i < perm_count; i ++)
  481. perm_table [i] = (int*) malloc (op_count * sizeof (int));
  482. return perm_table;
  483. }
  484. void free_perm_table (int ***perm_table, int op_count)
  485. {
  486. int i;
  487. int perm_count = get_perm_count (op_count);
  488. for (i = 0; i < perm_count; i ++)
  489. free ((*perm_table)[i]);
  490. free (*perm_table);
  491. *perm_table = NULL;
  492. }
  493. void generate_perm_table (int *elements, int element_count, int static_part,
  494. int **perm_table)
  495. {
  496. int i;
  497. int elements_copy [MAX_OPS];
  498. int start_pos;
  499. if (element_count - 1 == static_part)
  500. {
  501. memcpy (*perm_table, elements, element_count * sizeof (int));
  502. return;
  503. }
  504. start_pos = 0;
  505. for (i = 0; i < element_count - static_part; i ++)
  506. {
  507. memcpy (elements_copy, elements, element_count * sizeof (int));
  508. elements_copy [static_part] = elements [static_part + i];
  509. elements_copy [static_part + i] = elements [static_part];
  510. generate_perm_table (elements_copy, element_count, static_part + 1,
  511. &perm_table [start_pos]);
  512. start_pos += get_perm_count (element_count - static_part - 1);
  513. }
  514. }
  515. int get_perm_count (int op_count)
  516. {
  517. int i;
  518. int perm_count = 1;
  519. for (i = 2; i <= op_count; i ++)
  520. perm_count *= i;
  521. return perm_count;
  522. }
  523. void apply_add_operation (Entry_State *entry, Operation *op)
  524. {
  525. if (entry->values[op->value_index].presence_csn < op->csn)
  526. {
  527. entry->values[op->value_index].presence_csn = op->csn;
  528. resolve_value_state (entry, op->value_index);
  529. }
  530. }
  531. void apply_value_delete_operation (Entry_State *entry, Operation *op)
  532. {
  533. if (entry->values[op->value_index].delete_csn < op->csn)
  534. {
  535. entry->values[op->value_index].delete_csn = op->csn;
  536. resolve_value_state (entry, op->value_index);
  537. }
  538. }
  539. void apply_attr_delete_operation (Entry_State *entry, Operation *op)
  540. {
  541. int i;
  542. if (entry->attr_delete_csn < op->csn)
  543. {
  544. entry->attr_delete_csn = op->csn;
  545. for (i = 0; i < sim.value_count; i++)
  546. {
  547. resolve_value_state (entry, i);
  548. }
  549. }
  550. }
  551. void apply_rename_operation (Entry_State *entry, Operation *op)
  552. {
  553. int index;
  554. if (entry->values[op->value_index].presence_csn == NOT_PRESENT)
  555. entry->values[op->value_index].presence_csn = op->csn;
  556. index = dn_csn_add (entry, op->value_index, op->csn);
  557. if (index > 0)
  558. resolve_value_state (entry, entry->dn_csns[index - 1].value_index);
  559. resolve_value_state (entry, entry->dn_csns[index].value_index);
  560. }
  561. void purge_value_state (Entry_State *entry, int value_index)
  562. {
  563. Value_State *value = &(entry->values[value_index]);
  564. int value_distinguished_csn = get_value_dn_csn (entry, value_index);
  565. if (value_distinguished_csn == -1 && value->presence_csn > value->delete_csn)
  566. value->delete_csn = NOT_PRESENT;
  567. else if (value->delete_csn < max_val (value_distinguished_csn, value->presence_csn))
  568. value->delete_csn = NOT_PRESENT;
  569. }
  570. void resolve_value_state (Entry_State *entry, int value_index)
  571. {
  572. Value_State *value = &(entry->values[value_index]);
  573. int value_distinguished_csn = get_value_dn_csn (entry, value_index);
  574. purge_value_state (entry, value_index);
  575. /* no deletes that effect the state */
  576. if (max_val (value->delete_csn, entry->attr_delete_csn) <
  577. max_val (value_distinguished_csn, value->presence_csn))
  578. {
  579. value->present = 1;
  580. return;
  581. }
  582. if (value->present) /* check if it should be removed based on the current state */
  583. {
  584. if (!value_distinguished_at_delete (entry, value_index))
  585. {
  586. value->present = 0;
  587. }
  588. }
  589. else /* not present - check if it should be restored */
  590. {
  591. if (value_distinguished_at_delete (entry, value_index))
  592. {
  593. value->present = 1;
  594. }
  595. }
  596. }
  597. int value_distinguished_at_delete (Entry_State *entry, int value_index)
  598. {
  599. Value_State *value = &(entry->values[value_index]);
  600. int value_distinguished_csn = get_value_dn_csn (entry, value_index);
  601. int delete_csn;
  602. int i;
  603. /* value has never been distinguished */
  604. if (value_distinguished_csn == -1)
  605. return 0;
  606. delete_csn = max_val (entry->attr_delete_csn, value->delete_csn);
  607. for (i = 0; i < entry->dn_csn_count; i++)
  608. {
  609. if (entry->dn_csns[i].csn > delete_csn)
  610. break;
  611. }
  612. /* i is never equal to 0 because the csn of the first element is always
  613. smaller than csn of any operation we can receive */
  614. return (entry->dn_csns[i-1].value_index == value_index);
  615. }
  616. int compare_entry_state (Entry_State *entry1, Entry_State *entry2, int run)
  617. {
  618. int i;
  619. int error = 0;
  620. /* first - quick check for present / not present */
  621. for (i = 0; i < sim.value_count; i++)
  622. {
  623. if (entry1->values[i].present != entry2->values[i].present)
  624. {
  625. fprintf (sim.fout,
  626. "value %s is %s present in the first run but %s present in the %d run\n",
  627. g_values[i], entry1->values[i].present ? "" : "not",
  628. entry2->values[i].present ? "" : "not", run);
  629. error = 1;
  630. }
  631. }
  632. if (error)
  633. return 3;
  634. /* compare dnc_csn list */
  635. if (entry1->dn_csn_count != entry2->dn_csn_count)
  636. {
  637. fprintf (sim.fout, "dn_csn count is %d for run 1 and %d for run %d\n",
  638. entry1->dn_csn_count, entry2->dn_csn_count, run);
  639. error = 1;
  640. }
  641. for (i = 0; i < entry1->dn_csn_count; i++)
  642. {
  643. if (entry1->dn_csns [i].csn != entry2->dn_csns [i].csn ||
  644. entry1->dn_csns [i].value_index != entry2->dn_csns [i].value_index)
  645. {
  646. fprintf (sim.fout,"elements %d of dn csn list are different:\n"
  647. "\tfirst run: csn - %d, value - %s\n"
  648. "\t%d run: csn - %d, value - %s\n", i,
  649. entry1->dn_csns [i].csn,
  650. g_values[entry1->dn_csns [i].value_index],
  651. run, entry2->dn_csns [i].csn,
  652. g_values[entry2->dn_csns [i].value_index]);
  653. error = 1;
  654. }
  655. }
  656. /* compare value state */
  657. if (entry1->attr_delete_csn != entry2->attr_delete_csn)
  658. {
  659. fprintf (sim.fout, "attribute delete csn is %d for run 1 "
  660. "but is %d for run %d\n", entry1->attr_delete_csn,
  661. entry2->attr_delete_csn, run);
  662. error = 1;
  663. }
  664. for (i = 0; i < sim.value_count; i++)
  665. {
  666. if (entry1->values[i].presence_csn != entry2->values[i].presence_csn)
  667. {
  668. fprintf (sim.fout, "presence csn for value %s is %d in run 1 "
  669. "but is %d in run %d\n", g_values[i], entry1->values[i].presence_csn,
  670. entry2->values[i].presence_csn, run);
  671. error = 1;
  672. }
  673. if (entry1->values[i].distinguished_index != entry2->values[i].distinguished_index)
  674. {
  675. fprintf (sim.fout, "distinguished index for value %s is %d in run 1 "
  676. "but is %d in run %d\n", g_values[i],
  677. entry1->values[i].distinguished_index,
  678. entry2->values[i].distinguished_index, run);
  679. error = 1;
  680. }
  681. if (entry1->values[i].delete_csn != entry2->values[i].delete_csn)
  682. {
  683. fprintf (sim.fout, "delete csn for value %s is %d in run 1 "
  684. "but is %d in run %d\n", g_values[i], entry1->values[i].delete_csn,
  685. entry2->values[i].delete_csn, run);
  686. error = 1;
  687. }
  688. }
  689. if (error != 0)
  690. {
  691. return 1;
  692. }
  693. else
  694. return 0;
  695. }
  696. int dn_csn_add (Entry_State *entry, int value_index, int csn)
  697. {
  698. int i, j;
  699. int distinguished_index;
  700. for (i = 0; i < entry->dn_csn_count; i++)
  701. {
  702. if (entry->dn_csns[i].csn > csn)
  703. break;
  704. }
  705. if (i < entry->dn_csn_count)
  706. {
  707. distinguished_index = i;
  708. for (j = i; j < entry->dn_csn_count; j ++)
  709. {
  710. if (entry->dn_csns[j].value_index == value_index)
  711. distinguished_index = j + 1;
  712. if (entry->values [entry->dn_csns[j].value_index].distinguished_index == j)
  713. entry->values [entry->dn_csns[j].value_index].distinguished_index ++;
  714. }
  715. memcpy (&(entry->dn_csns[i+1]), &(entry->dn_csns[i]),
  716. (entry->dn_csn_count - i) * sizeof (Dn_Csn));
  717. }
  718. else
  719. {
  720. distinguished_index = entry->dn_csn_count;
  721. }
  722. entry->values[value_index].distinguished_index = distinguished_index;
  723. entry->dn_csns[i].csn = csn;
  724. entry->dn_csns[i].value_index = value_index;
  725. entry->dn_csn_count ++;
  726. return i;
  727. }
  728. int get_value_dn_csn (Entry_State *entry, int value_index)
  729. {
  730. Value_State *value = &(entry->values [value_index]);
  731. if (value->distinguished_index == -1)
  732. return -1;
  733. else
  734. return entry->dn_csns [value->distinguished_index].csn;
  735. }
  736. void dump_operations (Operation *ops, int op_count, int *order)
  737. {
  738. int index;
  739. int i;
  740. for (i = 0; i < op_count; i ++)
  741. {
  742. if (order == NULL) /* current order */
  743. index = i;
  744. else
  745. index = order [i];
  746. dump_operation (&ops[index]);
  747. }
  748. fprintf (sim.fout, "\n");
  749. }
  750. void dump_operation (Operation *op)
  751. {
  752. switch (op->type)
  753. {
  754. case OP_ADD_VALUE:
  755. fprintf (sim.fout, "\t%d add value %s\n", op->csn,
  756. g_values [op->value_index]);
  757. break;
  758. case OP_DELETE_VALUE:
  759. fprintf (sim.fout, "\t%d delete value %s\n", op->csn,
  760. g_values [op->value_index]);
  761. break;
  762. case OP_DELETE_ATTR:
  763. fprintf (sim.fout, "\t%d delete attribute\n", op->csn);
  764. break;
  765. case OP_RENAME_ENTRY:
  766. fprintf (sim.fout, "\t%d rename entry to %s\n", op->csn,
  767. g_values [op->value_index]);
  768. break;
  769. }
  770. }
  771. void dump_perm_table (int **perm_table, int op_count)
  772. {
  773. int i, j;
  774. int perm_count = get_perm_count (op_count);
  775. for (i = 0; i < op_count; i++)
  776. {
  777. for (j = 0; j < perm_count; j++)
  778. {
  779. fprintf (sim.fout, "%d ", perm_table [j][i]);
  780. }
  781. fprintf (sim.fout, "\n");
  782. }
  783. }
  784. void dump_entry_state (Entry_State *entry)
  785. {
  786. int i;
  787. dump_dn_csn_list (entry);
  788. for (i = 0; i < sim.value_count; i++)
  789. {
  790. dump_value_state (&(entry->values[i]));
  791. }
  792. fprintf (sim.fout, "\n");
  793. }
  794. void dump_value_state (Value_State *value)
  795. {
  796. fprintf (sim.fout, "\tvalue %s is %s\n", g_values[value->value_index],
  797. value->present ? "present" : "not present");
  798. if (sim.verbose)
  799. {
  800. fprintf (sim.fout, "\t\tpresent csn: %d\n", value->presence_csn);
  801. fprintf (sim.fout, "\t\tdistinguished index: %d\n", value->distinguished_index);
  802. fprintf (sim.fout, "\t\tdelete value csn: %d\n", value->delete_csn);
  803. }
  804. }
  805. void dump_dn_csn_list (Entry_State *entry)
  806. {
  807. int i;
  808. fprintf (sim.fout, "\tdn csn list: \n");
  809. for (i = 0; i < entry->dn_csn_count; i++)
  810. {
  811. fprintf (sim.fout, "\t\tvalue: %s, csn: %d\n",
  812. g_values[entry->dn_csns[i].value_index], entry->dn_csns[i].csn);
  813. }
  814. }
  815. /* misc functions */
  816. int max_val (int a, int b)
  817. {
  818. if (a >= b)
  819. return a;
  820. else
  821. return b;
  822. }