2 * Copyright (c) 2007-2015, SUSE LLC
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
11 * Transaction ordering
20 #include "transaction.h"
26 struct s_TransactionElement {
27 Id p; /* solvable id */
28 Id edges; /* pointer into edges data */
32 struct s_TransactionOrderdata {
33 struct s_TransactionElement *tes;
40 #define TYPE_BROKEN (1<<0)
41 #define TYPE_CON (1<<1)
43 #define TYPE_REQ_P (1<<2)
44 #define TYPE_PREREQ_P (1<<3)
46 #define TYPE_SUG (1<<4)
47 #define TYPE_REC (1<<5)
49 #define TYPE_REQ (1<<6)
50 #define TYPE_PREREQ (1<<7)
52 #define TYPE_CYCLETAIL (1<<16)
53 #define TYPE_CYCLEHEAD (1<<17)
55 #define EDGEDATA_BLOCK 127
58 transaction_clone_orderdata(Transaction *trans, Transaction *srctrans)
60 struct s_TransactionOrderdata *od = srctrans->orderdata;
63 trans->orderdata = solv_calloc(1, sizeof(*trans->orderdata));
64 trans->orderdata->tes = solv_memdup2(od->tes, od->ntes, sizeof(*od->tes));
65 trans->orderdata->ntes = od->ntes;
66 trans->orderdata->invedgedata = solv_memdup2(od->invedgedata, od->ninvedgedata, sizeof(Id));
67 trans->orderdata->ninvedgedata = od->ninvedgedata;
70 trans->orderdata->cycles = solv_calloc(1, sizeof(Queue));
71 queue_init_clone(trans->orderdata->cycles, od->cycles);
76 transaction_free_orderdata(Transaction *trans)
80 struct s_TransactionOrderdata *od = trans->orderdata;
81 od->tes = solv_free(od->tes);
82 od->invedgedata = solv_free(od->invedgedata);
85 queue_free(od->cycles);
86 od->cycles = solv_free(od->cycles);
88 trans->orderdata = solv_free(trans->orderdata);
94 struct s_TransactionElement *tes;
106 addteedge(struct orderdata *od, int from, int to, int type)
109 struct s_TransactionElement *te;
114 /* printf("edge %d(%s) -> %d(%s) type %x\n", from, pool_solvid2str(pool, od->tes[from].p), to, pool_solvid2str(pool, od->tes[to].p), type); */
117 for (i = te->edges; od->edgedata[i]; i += 2)
118 if (od->edgedata[i] == to)
122 od->edgedata[i + 1] |= type;
125 if (i + 1 == od->nedgedata)
127 /* printf("tail add %d\n", i - te->edges); */
130 od->edgedata = solv_extend(od->edgedata, od->nedgedata, 3, sizeof(Id), EDGEDATA_BLOCK);
134 /* printf("extend %d\n", i - te->edges); */
135 od->edgedata = solv_extend(od->edgedata, od->nedgedata, 3 + (i - te->edges), sizeof(Id), EDGEDATA_BLOCK);
137 memcpy(od->edgedata + od->nedgedata, od->edgedata + te->edges, sizeof(Id) * (i - te->edges));
138 i = od->nedgedata + (i - te->edges);
139 te->edges = od->nedgedata;
141 od->edgedata[i] = to;
142 od->edgedata[i + 1] = type;
143 od->edgedata[i + 2] = 0; /* end marker */
144 od->nedgedata = i + 3;
148 addedge(struct orderdata *od, Id from, Id to, int type)
150 Transaction *trans = od->trans;
151 Pool *pool = trans->pool;
153 struct s_TransactionElement *te;
156 /* printf("addedge %d %d type %d\n", from, to, type); */
157 s = pool->solvables + from;
158 if (s->repo == pool->installed && trans->transaction_installed[from - pool->installed->start])
160 /* obsolete, map to install */
161 if (trans->transaction_installed[from - pool->installed->start] > 0)
162 from = trans->transaction_installed[from - pool->installed->start];
168 queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
169 transaction_all_obs_pkgs(trans, from, &ti);
170 for (i = 0; i < ti.count; i++)
171 addedge(od, ti.elements[i], to, type);
176 s = pool->solvables + to;
177 if (s->repo == pool->installed && trans->transaction_installed[to - pool->installed->start])
179 /* obsolete, map to install */
180 if (trans->transaction_installed[to - pool->installed->start] > 0)
181 to = trans->transaction_installed[to - pool->installed->start];
187 queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
188 transaction_all_obs_pkgs(trans, to, &ti);
189 for (i = 0; i < ti.count; i++)
190 addedge(od, from, ti.elements[i], type);
196 /* map from/to to te numbers */
197 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
204 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
211 addteedge(od, from, to, type);
215 havescripts(Pool *pool, Id solvid)
217 Solvable *s = pool->solvables + solvid;
223 reqp = s->repo->idarraydata + s->requires;
224 while ((req = *reqp++) != 0)
226 if (req == SOLVABLE_PREREQMARKER)
233 dep = pool_id2str(pool, req);
234 if (*dep == '/' && strcmp(dep, "/sbin/ldconfig") != 0)
242 addsolvableedges(struct orderdata *od, Solvable *s)
244 Transaction *trans = od->trans;
245 Pool *pool = trans->pool;
247 int i, j, pre, numins;
248 Repo *installed = pool->installed;
254 printf("addsolvableedges %s\n", pool_solvable2str(pool, s));
256 p = s - pool->solvables;
261 reqp = s->repo->idarraydata + s->requires;
263 while ((req = *reqp++) != 0)
265 if (req == SOLVABLE_PREREQMARKER)
271 numins = 0; /* number of packages to be installed providing it */
272 provbyinst = 0; /* provided by kept package */
273 FOR_PROVIDES(p2, pp2, req)
275 s2 = pool->solvables + p2;
278 depq.count = 0; /* self provides */
281 if (s2->repo == installed && !MAPTST(&trans->transactsmap, p2))
286 if (s2->repo != installed && !MAPTST(&trans->transactsmap, p2))
287 continue; /* package stays uninstalled */
289 if (s->repo == installed)
291 /* s gets uninstalled */
292 queue_pushunique(&depq, p2);
293 if (s2->repo != installed)
298 if (s2->repo == installed)
299 continue; /* s2 gets uninstalled */
300 queue_pushunique(&depq, p2);
305 /* prune to harmless ->inst edges */
306 for (i = j = 0; i < depq.count; i++)
307 if (pool->solvables[depq.elements[i]].repo != installed)
308 depq.elements[j++] = depq.elements[i];
312 if (numins && depq.count)
314 if (s->repo == installed)
316 for (i = 0; i < depq.count; i++)
318 if (pool->solvables[depq.elements[i]].repo == installed)
320 for (j = 0; j < depq.count; j++)
322 if (pool->solvables[depq.elements[j]].repo != installed)
324 if (trans->transaction_installed[depq.elements[i] - pool->installed->start] == depq.elements[j])
325 continue; /* no self edge */
327 printf("add interrreq uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, depq.elements[i]), pool_dep2str(pool, req), pool_solvid2str(pool, depq.elements[j]));
329 addedge(od, depq.elements[i], depq.elements[j], pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
335 /* no mixed types, remove all deps on uninstalls */
336 for (i = j = 0; i < depq.count; i++)
337 if (pool->solvables[depq.elements[i]].repo != installed)
338 depq.elements[j++] = depq.elements[i];
341 for (i = 0; i < depq.count; i++)
343 p2 = depq.elements[i];
344 if (pool->solvables[p2].repo != installed)
346 /* all elements of depq are installs, thus have different TEs */
347 if (pool->solvables[p].repo != installed)
350 printf("add inst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, req), pool_solvid2str(pool, p2));
352 addedge(od, p, p2, pre);
357 printf("add uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, req), pool_solvid2str(pool, p2));
359 addedge(od, p, p2, pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
364 if (s->repo != installed)
365 continue; /* no inst->uninst edges, please! */
367 /* uninst -> uninst edge. Those make trouble. Only add if we must */
368 if (trans->transaction_installed[p - installed->start] && !havescripts(pool, p))
370 /* p is obsoleted by another package and has no scripts */
371 /* we assume that the obsoletor is good enough to replace p */
375 printf("add uninst->uninst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, req), pool_solvid2str(pool, p2));
377 addedge(od, p2, p, pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
385 conp = s->repo->idarraydata + s->conflicts;
386 while ((con = *conp++) != 0)
388 FOR_PROVIDES(p2, pp2, con)
392 s2 = pool->solvables + p2;
395 if (s->repo == installed)
397 if (s2->repo != installed && MAPTST(&trans->transactsmap, p2))
399 /* deinstall p before installing p2 */
401 printf("add conflict uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p2), pool_dep2str(pool, con), pool_solvid2str(pool, p));
403 addedge(od, p2, p, TYPE_CON);
408 if (s2->repo == installed && MAPTST(&trans->transactsmap, p2))
410 /* deinstall p2 before installing p */
412 printf("add conflict uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, con), pool_solvid2str(pool, p2));
414 addedge(od, p, p2, TYPE_CON);
421 if (s->recommends && s->repo != installed)
424 recp = s->repo->idarraydata + s->recommends;
425 while ((rec = *recp++) != 0)
428 FOR_PROVIDES(p2, pp2, rec)
430 s2 = pool->solvables + p2;
433 depq.count = 0; /* self provides */
436 if (s2->repo == installed && !MAPTST(&trans->transactsmap, p2))
438 if (s2->repo != installed && !MAPTST(&trans->transactsmap, p2))
439 continue; /* package stays uninstalled */
440 if (s2->repo != installed)
441 queue_pushunique(&depq, p2);
443 for (i = 0; i < depq.count; i++)
445 p2 = depq.elements[i];
446 if (pool->solvables[p2].repo != installed)
449 printf("add recommends inst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, rec), pool_solvid2str(pool, p2));
451 addedge(od, p, p2, TYPE_REC);
456 if (s->suggests && s->repo != installed)
459 sugp = s->repo->idarraydata + s->suggests;
460 while ((sug = *sugp++) != 0)
463 FOR_PROVIDES(p2, pp2, sug)
465 s2 = pool->solvables + p2;
468 depq.count = 0; /* self provides */
471 if (s2->repo == installed && !MAPTST(&trans->transactsmap, p2))
473 if (s2->repo != installed && !MAPTST(&trans->transactsmap, p2))
474 continue; /* package stays uninstalled */
475 if (s2->repo != installed)
476 queue_pushunique(&depq, p2);
478 for (i = 0; i < depq.count; i++)
480 p2 = depq.elements[i];
481 if (pool->solvables[p2].repo != installed)
484 printf("add suggests inst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, sug), pool_solvid2str(pool, p2));
486 addedge(od, p, p2, TYPE_SUG);
491 if (s->repo == installed && solvable_lookup_idarray(s, SOLVABLE_TRIGGERS, &depq) && depq.count)
493 /* we're getting deinstalled/updated. Try to do this before our
494 * triggers are hit */
495 for (i = 0; i < depq.count; i++)
497 Id tri = depq.elements[i];
498 FOR_PROVIDES(p2, pp2, tri)
502 s2 = pool->solvables + p2;
505 if (s2->name == s->name)
506 continue; /* obsoleted anyway */
507 if (s2->repo != installed && MAPTST(&trans->transactsmap, p2))
509 /* deinstall/update p before installing p2 */
511 printf("add trigger uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p2), pool_dep2str(pool, tri), pool_solvid2str(pool, p));
513 addedge(od, p2, p, TYPE_CON);
522 /* break an edge in a cycle */
524 breakcycle(struct orderdata *od, Id *cycle)
526 Pool *pool = od->trans->pool;
527 Id ddegmin, ddegmax, ddeg;
529 struct s_TransactionElement *te;
532 ddegmin = ddegmax = 0;
533 for (k = 0; cycle[k + 1]; k += 2)
535 ddeg = od->edgedata[cycle[k + 1] + 1];
538 if (!k || ddeg < ddegmin)
546 if (havescripts(pool, od->tes[cycle[l]].p) && !havescripts(pool, od->tes[cycle[k]].p))
548 /* prefer k, as l comes from a package with contains scriptlets */
552 /* same edge value, check for prereq */
556 /* record brkoen cycle starting with the tail */
557 queue_push(&od->cycles, od->cyclesdata.count); /* offset into data */
558 queue_push(&od->cycles, k / 2); /* cycle elements */
559 queue_push(&od->cycles, od->edgedata[cycle[l + 1] + 1]); /* broken edge */
560 queue_push(&od->cycles, (ddegmax << 16) | ddegmin); /* max/min values */
567 queue_push(&od->cyclesdata, cycle[k]);
571 queue_push(&od->cyclesdata, 0); /* mark end */
573 /* break that edge */
574 od->edgedata[cycle[l + 1] + 1] |= TYPE_BROKEN;
577 if (ddegmin < TYPE_REQ)
581 /* cycle recorded, print it */
582 if (ddegmin >= TYPE_REQ && (ddegmax & TYPE_PREREQ) != 0)
583 POOL_DEBUG(SOLV_DEBUG_STATS, "CRITICAL ");
584 POOL_DEBUG(SOLV_DEBUG_STATS, "cycle: --> ");
585 for (k = 0; cycle[k + 1]; k += 2)
587 te = od->tes + cycle[k];
588 if ((od->edgedata[cycle[k + 1] + 1] & TYPE_BROKEN) != 0)
589 POOL_DEBUG(SOLV_DEBUG_STATS, "%s ##%x##> ", pool_solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
591 POOL_DEBUG(SOLV_DEBUG_STATS, "%s --%x--> ", pool_solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
593 POOL_DEBUG(SOLV_DEBUG_STATS, "\n");
598 dump_tes(struct orderdata *od)
600 Pool *pool = od->trans->pool;
603 struct s_TransactionElement *te, *te2;
606 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
608 Solvable *s = pool->solvables + te->p;
609 POOL_DEBUG(SOLV_DEBUG_RESULT, "TE %4d: %c%s\n", i, s->repo == pool->installed ? '-' : '+', pool_solvable2str(pool, s));
610 if (s->repo != pool->installed)
613 transaction_all_obs_pkgs(od->trans, te->p, &obsq);
614 for (j = 0; j < obsq.count; j++)
615 POOL_DEBUG(SOLV_DEBUG_RESULT, " -%s\n", pool_solvid2str(pool, obsq.elements[j]));
617 for (j = te->edges; od->edgedata[j]; j += 2)
619 te2 = od->tes + od->edgedata[j];
620 if ((od->edgedata[j + 1] & TYPE_BROKEN) == 0)
621 POOL_DEBUG(SOLV_DEBUG_RESULT, " --%x--> TE %4d: %s\n", od->edgedata[j + 1], od->edgedata[j], pool_solvid2str(pool, te2->p));
623 POOL_DEBUG(SOLV_DEBUG_RESULT, " ##%x##> TE %4d: %s\n", od->edgedata[j + 1], od->edgedata[j], pool_solvid2str(pool, te2->p));
630 reachable(struct orderdata *od, Id i)
632 struct s_TransactionElement *te = od->tes + i;
638 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
640 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
642 if (!od->tes[k].mark)
644 if (od->tes[k].mark == 2)
654 addcycleedges(struct orderdata *od, Id *cycle, Queue *todo)
657 Transaction *trans = od->trans;
658 Pool *pool = trans->pool;
660 struct s_TransactionElement *te;
665 printf("addcycleedges\n");
666 for (i = 0; (j = cycle[i]) != 0; i++)
667 printf("cycle %s\n", pool_solvid2str(pool, od->tes[j].p));
670 /* first add all the tail cycle edges */
672 /* see what we can reach from the cycle */
674 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
676 for (i = 0; (j = cycle[i]) != 0; i++)
678 od->tes[j].mark = -1;
687 te->mark = te->mark < 0 ? 2 : 1;
688 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
690 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
692 if (od->tes[k].mark > 0)
693 continue; /* no need to visit again */
697 /* now all cycle TEs are marked with 2, all TEs reachable
698 * from the cycle are marked with 1 */
700 od->tes[tail].mark = 1; /* no need to add edges */
702 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
705 continue; /* reachable from cycle */
706 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
708 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
710 if (od->tes[k].mark != 2)
712 /* We found an edge to the cycle. Add an extra edge to the tail */
713 /* the TE was not reachable, so we're not creating a new cycle! */
715 printf("adding TO TAIL cycle edge %d->%d %s->%s!\n", i, tail, pool_solvid2str(pool, od->tes[i].p), pool_solvid2str(pool, od->tes[tail].p));
717 j -= te->edges; /* in case we move */
718 addteedge(od, i, tail, TYPE_CYCLETAIL);
720 break; /* one edge is enough */
724 /* now add all head cycle edges */
727 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
730 for (i = 0; (j = cycle[i]) != 0; i++)
735 /* first the head to save some time */
737 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
739 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
741 if (!od->tes[k].mark)
743 if (od->tes[k].mark == -1)
744 od->tes[k].mark = -2; /* no need for another edge */
746 for (i = 0; cycle[i] != 0; i++)
748 if (cycle[i] == head)
750 te = od->tes + cycle[i];
751 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
753 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
755 /* see if we can reach a cycle TE from k */
756 if (!od->tes[k].mark)
758 if (od->tes[k].mark == -1)
761 printf("adding FROM HEAD cycle edge %d->%d %s->%s [%s]!\n", head, k, pool_solvid2str(pool, od->tes[head].p), pool_solvid2str(pool, od->tes[k].p), pool_solvid2str(pool, od->tes[cycle[i]].p));
763 addteedge(od, head, k, TYPE_CYCLEHEAD);
764 od->tes[k].mark = -2; /* no need to add that one again */
771 transaction_order(Transaction *trans, int flags)
773 Pool *pool = trans->pool;
774 Queue *tr = &trans->steps;
775 Repo *installed = pool->installed;
778 int i, j, k, numte, numedge;
780 struct s_TransactionElement *te;
781 Queue todo, obsq, samerepoq, uninstq;
790 start = now = solv_timems(0);
791 POOL_DEBUG(SOLV_DEBUG_STATS, "ordering transaction\n");
792 /* free old data if present */
793 if (trans->orderdata)
795 struct s_TransactionOrderdata *od = trans->orderdata;
796 od->tes = solv_free(od->tes);
797 od->invedgedata = solv_free(od->invedgedata);
798 trans->orderdata = solv_free(trans->orderdata);
801 /* create a transaction element for every active component */
803 for (i = 0; i < tr->count; i++)
806 s = pool->solvables + p;
807 if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
811 POOL_DEBUG(SOLV_DEBUG_STATS, "transaction elements: %d\n", numte);
813 return; /* nothing to do... */
815 numte++; /* leave first one zero */
816 memset(&od, 0, sizeof(od));
819 od.tes = solv_calloc(numte, sizeof(*od.tes));
820 od.edgedata = solv_extend(0, 0, 1, sizeof(Id), EDGEDATA_BLOCK);
823 queue_init(&od.cycles);
826 for (i = 0, te = od.tes + 1; i < tr->count; i++)
829 s = pool->solvables + p;
830 if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
836 /* create dependency graph */
837 for (i = 0; i < tr->count; i++)
838 addsolvableedges(&od, pool->solvables + tr->elements[i]);
842 for (i = 1, te = od.tes + i; i < numte; i++, te++)
843 for (j = te->edges; od.edgedata[j]; j += 2)
845 POOL_DEBUG(SOLV_DEBUG_STATS, "edges: %d, edge space: %d\n", numedge, od.nedgedata / 2);
846 POOL_DEBUG(SOLV_DEBUG_STATS, "edge creation took %d ms\n", solv_timems(now));
852 now = solv_timems(0);
853 /* kill all cycles */
855 for (i = numte - 1; i > 0; i--)
856 queue_push(&todo, i);
860 i = queue_pop(&todo);
861 /* printf("- look at TE %d\n", i); */
865 od.tes[i].mark = 2; /* done with that one */
870 continue; /* already finished before */
873 int edgestovisit = 0;
874 /* new node, visit edges */
875 for (j = te->edges; (k = od.edgedata[j]) != 0; j += 2)
877 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
879 if (od.tes[k].mark == 2)
880 continue; /* no need to visit again */
882 queue_push(&todo, -i); /* end of edges marker */
883 queue_push(&todo, k);
886 te->mark = 2; /* no edges, done with that one */
888 te->mark = 1; /* under investigation */
891 /* oh no, we found a cycle */
892 /* find start of cycle node (<0) */
893 for (j = todo.count - 1; j >= 0; j--)
894 if (todo.elements[j] == -i)
898 /* build te/edge chain */
900 for (j = k; j < todo.count; j++)
901 if (todo.elements[j] < 0)
902 todo.elements[k++] = -todo.elements[j];
903 cycel = k - cycstart;
905 /* make room for edges, two extra element for cycle loop + terminating 0 */
906 while (todo.count < cycstart + 2 * cycel + 2)
907 queue_push(&todo, 0);
908 cycle = todo.elements + cycstart;
909 cycle[cycel] = i; /* close the loop */
910 cycle[2 * cycel + 1] = 0; /* terminator */
911 for (k = cycel; k > 0; k--)
913 cycle[k * 2] = cycle[k];
914 te = od.tes + cycle[k - 1];
915 assert(te->mark == 1);
916 te->mark = 0; /* reset investigation marker */
917 /* printf("searching for edge from %d to %d\n", cycle[k - 1], cycle[k]); */
918 for (j = te->edges; od.edgedata[j]; j += 2)
919 if (od.edgedata[j] == cycle[k])
921 assert(od.edgedata[j]);
922 cycle[k * 2 - 1] = j;
924 /* now cycle looks like this: */
925 /* te1 edge te2 edge te3 ... teN edge te1 0 */
926 breakcycle(&od, cycle);
927 /* restart with start of cycle */
928 todo.count = cycstart + 1;
930 POOL_DEBUG(SOLV_DEBUG_STATS, "cycles broken: %d\n", od.ncycles);
931 POOL_DEBUG(SOLV_DEBUG_STATS, "cycle breaking took %d ms\n", solv_timems(now));
933 now = solv_timems(0);
934 /* now go through all broken cycles and create cycle edges to help
936 for (i = od.cycles.count - 4; i >= 0; i -= 4)
938 if (od.cycles.elements[i + 2] >= TYPE_REQ)
939 addcycleedges(&od, od.cyclesdata.elements + od.cycles.elements[i], &todo);
941 for (i = od.cycles.count - 4; i >= 0; i -= 4)
943 if (od.cycles.elements[i + 2] < TYPE_REQ)
944 addcycleedges(&od, od.cyclesdata.elements + od.cycles.elements[i], &todo);
946 POOL_DEBUG(SOLV_DEBUG_STATS, "cycle edge creation took %d ms\n", solv_timems(now));
951 /* all edges are finally set up and there are no cycles, now the easy part.
952 * Create an ordered transaction */
953 now = solv_timems(0);
954 /* first invert all edges */
955 for (i = 1, te = od.tes + i; i < numte; i++, te++)
956 te->mark = 1; /* term 0 */
957 for (i = 1, te = od.tes + i; i < numte; i++, te++)
959 for (j = te->edges; od.edgedata[j]; j += 2)
961 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
963 od.tes[od.edgedata[j]].mark++;
967 for (i = 1, te = od.tes + i; i < numte; i++, te++)
972 POOL_DEBUG(SOLV_DEBUG_STATS, "invedge space: %d\n", j + 1);
973 od.invedgedata = solv_calloc(j + 1, sizeof(Id));
974 for (i = 1, te = od.tes + i; i < numte; i++, te++)
976 for (j = te->edges; od.edgedata[j]; j += 2)
978 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
980 od.invedgedata[--od.tes[od.edgedata[j]].mark] = i;
983 for (i = 1, te = od.tes + i; i < numte; i++, te++)
984 te->edges = te->mark; /* edges now points into invedgedata */
985 od.edgedata = solv_free(od.edgedata);
986 od.nedgedata = j + 1;
988 /* now the final ordering */
989 for (i = 1, te = od.tes + i; i < numte; i++, te++)
991 for (i = 1, te = od.tes + i; i < numte; i++, te++)
992 for (j = te->edges; od.invedgedata[j]; j++)
993 od.tes[od.invedgedata[j]].mark++;
995 queue_init(&samerepoq);
996 queue_init(&uninstq);
998 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1001 if (installed && pool->solvables[te->p].repo == installed)
1002 queue_push(&uninstq, i);
1004 queue_push(&todo, i);
1006 assert(todo.count > 0 || uninstq.count > 0);
1007 oldcount = tr->count;
1014 temedianr = solv_calloc(numte, sizeof(Id));
1015 for (i = 1; i < numte; i++)
1017 Solvable *s = pool->solvables + od.tes[i].p;
1018 if (installed && s->repo == installed)
1021 j = solvable_lookup_num(s, SOLVABLE_MEDIANR, 1);
1026 /* select an TE i */
1028 i = queue_shift(&uninstq);
1029 else if (samerepoq.count)
1030 i = queue_shift(&samerepoq);
1031 else if (todo.count)
1033 /* find next repo/media */
1034 for (j = 0; j < todo.count; j++)
1036 if (!j || temedianr[todo.elements[j]] < lastmedia)
1039 lastmedia = temedianr[todo.elements[j]];
1042 lastrepo = pool->solvables[od.tes[todo.elements[i]].p].repo;
1044 /* move all matching TEs to samerepoq */
1045 for (i = j = 0; j < todo.count; j++)
1047 int k = todo.elements[j];
1048 if (temedianr[k] == lastmedia && pool->solvables[od.tes[k].p].repo == lastrepo)
1049 queue_push(&samerepoq, k);
1051 todo.elements[i++] = k;
1055 assert(samerepoq.count);
1056 i = queue_shift(&samerepoq);
1062 queue_push(tr, te->p);
1064 printf("do %s [%d]\n", pool_solvid2str(pool, te->p), temedianr[i]);
1066 for (j = te->edges; od.invedgedata[j]; j++)
1068 struct s_TransactionElement *te2 = od.tes + od.invedgedata[j];
1069 assert(te2->mark > 0);
1070 if (--te2->mark == 0)
1072 Solvable *s = pool->solvables + te2->p;
1074 printf("free %s [%d]\n", pool_solvid2str(pool, te2->p), temedianr[od.invedgedata[j]]);
1076 if (installed && s->repo == installed)
1077 queue_push(&uninstq, od.invedgedata[j]);
1078 else if (s->repo == lastrepo && temedianr[od.invedgedata[j]] == lastmedia)
1079 queue_push(&samerepoq, od.invedgedata[j]);
1081 queue_push(&todo, od.invedgedata[j]);
1085 solv_free(temedianr);
1087 queue_free(&samerepoq);
1088 queue_free(&uninstq);
1090 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1091 assert(te->mark == 0);
1093 /* add back obsoleted packages */
1094 transaction_add_obsoleted(trans);
1095 assert(tr->count == oldcount);
1097 POOL_DEBUG(SOLV_DEBUG_STATS, "creating new transaction took %d ms\n", solv_timems(now));
1098 POOL_DEBUG(SOLV_DEBUG_STATS, "transaction ordering took %d ms\n", solv_timems(start));
1100 if ((flags & (SOLVER_TRANSACTION_KEEP_ORDERDATA | SOLVER_TRANSACTION_KEEP_ORDERCYCLES)) != 0)
1102 struct s_TransactionOrderdata *tod;
1103 trans->orderdata = tod = solv_calloc(1, sizeof(*trans->orderdata));
1104 if ((flags & SOLVER_TRANSACTION_KEEP_ORDERCYCLES) != 0)
1106 Queue *cycles = tod->cycles = solv_calloc(1, sizeof(Queue));
1107 queue_init_clone(cycles, &od.cyclesdata);
1108 /* map from tes to packages */
1109 for (i = 0; i < cycles->count; i++)
1110 if (cycles->elements[i])
1111 cycles->elements[i] = od.tes[cycles->elements[i]].p;
1112 queue_insertn(cycles, cycles->count, od.cycles.count, od.cycles.elements);
1113 queue_push(cycles, od.cycles.count / 4);
1115 if ((flags & SOLVER_TRANSACTION_KEEP_ORDERDATA) != 0)
1119 tod->invedgedata = od.invedgedata;
1120 tod->ninvedgedata = od.nedgedata;
1126 solv_free(od.invedgedata);
1127 queue_free(&od.cycles);
1128 queue_free(&od.cyclesdata);
1133 transaction_order_add_choices(Transaction *trans, Id chosen, Queue *choices)
1136 struct s_TransactionOrderdata *od = trans->orderdata;
1137 struct s_TransactionElement *te;
1140 return choices->count;
1143 /* initialization step */
1144 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1146 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1148 for (j = te->edges; od->invedgedata[j]; j++)
1149 od->tes[od->invedgedata[j]].mark++;
1151 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1153 queue_push(choices, te->p);
1154 return choices->count;
1156 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1157 if (te->p == chosen)
1160 return choices->count;
1163 /* hey! out-of-order installation! */
1166 for (j = te->edges; od->invedgedata[j]; j++)
1168 te = od->tes + od->invedgedata[j];
1169 assert(te->mark > 0 || te->mark == -1);
1170 if (te->mark > 0 && --te->mark == 0)
1171 queue_push(choices, te->p);
1173 return choices->count;
1177 transaction_add_obsoleted(Transaction *trans)
1179 Pool *pool = trans->pool;
1180 Repo *installed = pool->installed;
1187 if (!installed || !trans->steps.count)
1189 /* calculate upper bound */
1191 FOR_REPO_SOLVABLES(installed, p, s)
1192 if (MAPTST(&trans->transactsmap, p))
1197 steps = &trans->steps;
1198 queue_insertn(steps, 0, max, 0);
1201 map_init(&done, installed->end - installed->start);
1203 for (j = 0, i = max; i < steps->count; i++)
1205 p = trans->steps.elements[i];
1206 if (pool->solvables[p].repo == installed)
1208 if (!trans->transaction_installed[p - pool->installed->start])
1209 trans->steps.elements[j++] = p;
1212 trans->steps.elements[j++] = p;
1214 transaction_all_obs_pkgs(trans, p, &obsq);
1215 for (k = 0; k < obsq.count; k++)
1217 p = obsq.elements[k];
1218 assert(p >= installed->start && p < installed->end);
1219 if (!MAPTST(&trans->transactsmap, p)) /* just in case */
1221 if (MAPTST(&done, p - installed->start))
1223 MAPSET(&done, p - installed->start);
1224 trans->steps.elements[j++] = p;
1228 /* free unneeded space */
1229 queue_truncate(steps, j);
1235 transaction_check_pkg(Transaction *trans, Id tepkg, Id pkg, Map *ins, Map *seen, int onlyprereq, Id noconfpkg, int depth)
1237 Pool *pool = trans->pool;
1242 if (MAPTST(seen, pkg))
1245 s = pool->solvables + pkg;
1247 printf("- %*s%c%s\n", depth * 2, "", s->repo == pool->installed ? '-' : '+', pool_solvable2str(pool, s));
1253 reqp = s->repo->idarraydata + s->requires;
1254 while ((req = *reqp++) != 0)
1256 if (req == SOLVABLE_PREREQMARKER)
1261 if (onlyprereq && !inpre)
1263 if (!strncmp(pool_id2str(pool, req), "rpmlib(", 7))
1266 /* first check kept packages, then freshly installed, then not yet uninstalled */
1267 FOR_PROVIDES(p, pp, req)
1269 if (!MAPTST(ins, p))
1271 if (MAPTST(&trans->transactsmap, p))
1274 transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
1278 FOR_PROVIDES(p, pp, req)
1280 if (!MAPTST(ins, p))
1282 if (pool->solvables[p].repo == pool->installed)
1285 transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
1290 FOR_PROVIDES(p, pp, req)
1292 if (!MAPTST(ins, p))
1295 transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
1300 POOL_DEBUG(SOLV_DEBUG_RESULT, " %c%s: nothing provides %s needed by %c%s\n", pool->solvables[tepkg].repo == pool->installed ? '-' : '+', pool_solvid2str(pool, tepkg), pool_dep2str(pool, req), s->repo == pool->installed ? '-' : '+', pool_solvable2str(pool, s));
1307 transaction_check_order(Transaction *trans)
1309 Pool *pool = trans->pool;
1315 POOL_DEBUG(SOLV_DEBUG_RESULT, "\nchecking transaction order...\n");
1316 map_init(&ins, pool->nsolvables);
1317 map_init(&seen, pool->nsolvables);
1318 if (pool->installed)
1320 FOR_REPO_SOLVABLES(pool->installed, p, s)
1324 for (i = 0; i < trans->steps.count; i++)
1326 p = trans->steps.elements[i];
1327 s = pool->solvables + p;
1328 if (s->repo != pool->installed)
1330 if (s->repo != pool->installed)
1332 if (havescripts(pool, p))
1335 transaction_check_pkg(trans, p, p, &ins, &seen, 1, lastins, 0);
1337 if (s->repo == pool->installed)
1342 POOL_DEBUG(SOLV_DEBUG_RESULT, "transaction order check done.\n");
1346 transaction_order_get_cycleids(Transaction *trans, Queue *q, int minseverity)
1348 struct s_TransactionOrderdata *od = trans->orderdata;
1350 int i, cid, ncycles;
1353 if (!od || !od->cycles || !od->cycles->count)
1356 ncycles = cq->elements[cq->count - 1];
1357 i = cq->count - 1 - ncycles * 4;
1358 for (cid = 1; cid <= ncycles; cid++, i += 4)
1362 int cmin = cq->elements[i + 3] & 0xffff;
1363 int cmax = (cq->elements[i + 3] >> 16) & 0xffff;
1364 if (minseverity >= SOLVER_ORDERCYCLE_NORMAL && cmin < TYPE_REQ)
1366 if (minseverity >= SOLVER_ORDERCYCLE_CRITICAL && (cmax & TYPE_PREREQ) == 0)
1374 transaction_order_get_cycle(Transaction *trans, Id cid, Queue *q)
1376 struct s_TransactionOrderdata *od = trans->orderdata;
1378 int cmin, cmax, severity;
1382 if (!od || !od->cycles || !od->cycles->count)
1383 return SOLVER_ORDERCYCLE_HARMLESS;
1385 ncycles = cq->elements[cq->count - 1];
1386 if (cid < 1 || cid > ncycles)
1387 return SOLVER_ORDERCYCLE_HARMLESS;
1388 cid = cq->count - 1 - 4 * (ncycles - cid + 1);
1389 cmin = cq->elements[cid + 3] & 0xffff;
1390 cmax = (cq->elements[cid + 3] >> 16) & 0xffff;
1391 if (cmin < TYPE_REQ)
1392 severity = SOLVER_ORDERCYCLE_HARMLESS;
1393 else if ((cmax & TYPE_PREREQ) == 0)
1394 severity = SOLVER_ORDERCYCLE_NORMAL;
1396 severity = SOLVER_ORDERCYCLE_CRITICAL;
1398 queue_insertn(q, 0, cq->elements[cid + 1], cq->elements + cq->elements[cid]);